在Matlab中删除前18行文本文件

时间:2015-02-03 16:06:27

标签: matlab for-loop text-files lines delete-row

我一直在为我的研究项目工作。我有一个文本文件,前18行有以下信息:

Person  Study, Run1102
Sex M
Born    
Code    4E8F-58F4-0F68

Record  07-10-2011 Preferred01
Application trplatf
Creation date   07/10/2011 16:33:24
Exercises   {
#   Name    Start,sec   Length,sec  Start time
1   Gait    0.00    90.04
}

Frequency,Hz    100.000
Count   9004

Time,ms Position,m  Butterfly.force,N   Butterfly.x,mm  Butterfly.y,mm
Calibr      

其余信息,约9000行,格式如下:

0   0.022   62.07   92.43   0.00
10  0.045   0.00    NaN NaN
20  0.070   0.00    NaN NaN
30  0.096   0.00    NaN NaN

我需要找出一个,如何正确地将文件加载到Matlab中,然后删除18行。我不确定是否应该使用for循环或matlab中的函数。

不知道我拥有什么,但到目前为止这是我的代码;

fid1=fopen('','rt');
fid2=fopen('formattedFile','wt');
nSkip=18; % number records; use input() if doing often and not fixed
for idx=1:nSkip
   l=fgetl(fid1);
end
% rest of file
while ~feof(fid1)
    l=fgetl(fid1);
    fprintf(fid2,'%s\n',l);
end
fid1=fclose(fid1);
fid2=fclose(fid2);

任何形式的指导都会有所帮助!我是Matlab的新人......所以,对不起,如果我的代码毫无意义,我一直在网上寻求帮助。

1 个答案:

答案 0 :(得分:1)

您可以像这样阅读文件的所有行:

fid = fopen(filename);
Rows = textscan(fid, '%s', 'delimiter', '\n');
fclose(fid);

创建带有行的单元格数组。

然后,您可以扫描第19行的列,如下所示:

Columns= cellfun(@(x) textscan(x,'%f','delimiter','\t','CollectOutput',1), Rows{1,1}(19:end, :));

如果行的大小一致,您应该可以将其转换为cell2mat的数组。