我有一个包含2列的大型文本文件,其中的值以逗号分隔。我正在尝试创建一个简单的程序,允许连续绘制每3行提取的数据的图形,直到到达文件的末尾。
我的文件的前9行可以在下面看到:
115,1.2
324,3.4
987,1.2
435,-2.3
234,1.4
278,1.3
768,3.4
345,-1.3
126,3.6
我一直在阅读'Textread',我可以将数据写入多个输出,然后我可以使用'plot',在图表上绘制先前生成的输出。我知道我需要一些循环来重复这个过程并指出文件的结尾等等。但是我很难找到这样做的方法: - (。
我只管理了我文件前3行的图表(见下面的代码),但我需要重复这个过程,直到文件结束。
[Codes,Values]=textread('MyData.txt','%3u %f',3,'delimiter',',','emptyvalue',NAN); %//Reads the first three rows of my file and stores the values in 2 variables
figure
plot(Codes,Values) %//plots a graph with the variables obtained before
saveas(gcf,'Graph.pdf') %//Saves the created graph in a pdf format file.
如果有人能帮助我,我将非常感激。
答案 0 :(得分:0)
您提到它是一个大型文本文件,但是加载整个文本文件然后只是对第三行进行采样是一个问题,例如,如下所示:
[Codes, Values] = textread('MyData.txt','%3u %f','delimiter',',','emptyvalue',NAN);
rowstokeep = 1:3:length(Values) % every third line
plot(Codes{rowstokeep}, Values{rowstokeep}); % plot just the rows you wanted
答案 1 :(得分:0)
我终于找到了办法。我在这里粘贴代码,允许我从文本文件中每三行绘制一个图形。
[Codes,Values]=textread('MyData.txt','%3u %f','delimiter',',','emptyvalue',NAN); %//Reads my text file and stores the values in 2 variables
nrows=1;
conta=1;
contb=3;
contc=1;
contd=3;
nfig=1;
while nrows<=size(Codes,1)
if contb<=size(Codes,1)
Codes1=Codes(conta:contb,1);
Values1=Values(contc:contd,1);
figure
plot(Codes1,Values1) %//plots a graph with the selected rows.
saveas(gcf,strcat('Graph', num2str(nfig),'.pdf')) %//Saves the created graph in a pdf format file.
else
Codes1=Codes(conta:contb,1);
Values1=Values(contc:contd,1);
figure
plot(Codes1,Values1) %//plots a graph with the selected rows.
saveas(gcf,strcat('Graph', num2str(nfig),'.pdf'))
end
nrows=nrows+3;
conta=conta+3;
contb=contb+3;
contc=contc+3;
contd=contd+3;
nfig=nfig+1;
end