Textscan loop to store multiple groups of data

I would like to plot data from a file. My 'results.dat' file contains multiple groups of data as below -
51109, 753250
51365, 675792
-
51877, 897964
60716, 149582
60341, 123422
-
58586, 180476
56504, 235721
-
33023, 191901
30632, 193063
-
18428, 192009
12029, 235773
Groups are separated using '-'. I would like to do something like this:
while 1
%textscan until '-', i.e., loop when '-' is found
s(i) =textscan(fid,'%d, %d'); %I know this is wrong
i++;
end
So that s{0} stores the first group, s{1} the second group and so on. Then I will be able to plot as -
plot(s{0}{1},s{0}{2}); % x values: 51109, 51365; y values: 753250, 675792
plot(s{0}{1},s{0}{2}); % x values: 51877, 60716, 60341; y values: 897964, 149582, 123422

回答(1 个)

opt = {'Delimiter',','};
fmt = '%f%f';
out = {};
[fid,msg] = fopen('test.txt','rt');
assert(fid>=3,msg)
while ~feof(fid)
out(:,end+1) = textscan(fid,fmt,opt{:}); %#ok<SAGROW>
end
fclose(fid);
plot(out{:})
generates this plot:
And was tested on this file:

2 个评论

Hi, thanks for your answer. I ran your code with test.txt, the program runs for infinity! Its never coming out of the while loop. Can you help?
@Md Mamunur Rashid Akand: are you running my code exactly as it is above, or did you make changes? How are you running it? Are you using test.txt from my answer, or another file that you defined?

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by