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 件)

Stephen23
Stephen23 2017 年 4 月 11 日
編集済み: Stephen23 2017 年 4 月 11 日

0 投票

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 件のコメント

Md Mamunur Rashid Akand
Md Mamunur Rashid Akand 2017 年 4 月 11 日
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?
Stephen23
Stephen23 2017 年 4 月 11 日
@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 についてさらに検索

タグ

質問済み:

2017 年 4 月 10 日

コメント済み:

2017 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by