在Matlab中删除前18行文本文件
我一直在为我的研究项目做这个任务。我有一个文本文件,前18行包含以下信息:在Matlab中删除前18行文本文件,matlab,for-loop,text-files,lines,delete-row,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
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的新手。。。因此,如果我的代码没有意义,很抱歉,我一直在联机搜索帮助。您可以像这样读取文件中的所有行:
fid = fopen(filename);
Rows = textscan(fid, '%s', 'delimiter', '\n');
fclose(fid);
Columns= cellfun(@(x) textscan(x,'%f','delimiter','\t','CollectOutput',1), Rows{1,1}(19:end, :));
这将创建一个包含行的单元格数组
然后可以像这样扫描第19行中的列:
fid = fopen(filename);
Rows = textscan(fid, '%s', 'delimiter', '\n');
fclose(fid);
Columns= cellfun(@(x) textscan(x,'%f','delimiter','\t','CollectOutput',1), Rows{1,1}(19:end, :));
如果行的大小一致,您应该能够将其转换为具有
cell2mat
的数组,因此,我这样做了,它至少运行了!但我不确定它是否有效,我的输出是columns=empty matrix 0-by-1。。。我是否可以查看测试,看看它是否正常工作?@thewildone26行是否正确读取并存储在行{1,1}
?@thewildone26我刚刚尝试使用您的文件,它正常工作。实际上,我删除了Treatasempty
参数,因为它不是必需的。