【发布时间】:2017-09-21 13:25:23
【问题描述】:
我有一个有很多行的文本文件,每行都有一个数字。要绘制一条我想要的线,我需要阅读第 1、第 2 和第 7、第 8 和第 13、第 14 行和 s.o。我如何为此编写代码?
我从互联网上得到了一些东西,但我不知道如何使用以下代码实现我的问题。
fileID = fopen([fname 'r'],'r');
% initialize a counter
count = 0;
% keep reading the file
while 1
% get a line of text
R = fgetl(fileID);
count = count + 1;
% exit if the line is empty
if R == -1
break;
end
% check modulus of count for every 2nd and 11th line
if mod(count,11) == 1
tline_2nd = R;
elseif mod(count,2) == 10
tline_11th = R;
end
end
【问题讨论】:
-
行是否以数字开头,或者您只需要文件中的第 [1:6:end, 2:6:end] 行?您是要在读取后立即使用行中的数据然后丢弃,还是要存储所选行的所有数据?