在MATLAB中:如何从文本文件中每隔3行绘制一个图形?
我有一个大的文本文件,有两列,其中的值用逗号分隔。我正在尝试创建一个简单的程序,它允许绘制一个图形,连续提取每3行的数据,直到到达文件末尾在MATLAB中:如何从文本文件中每隔3行绘制一个图形?,matlab,file,graph,plot,repeat,Matlab,File,Graph,Plot,Repeat,我有一个大的文本文件,有两列,其中的值用逗号分隔。我正在尝试创建一个简单的程序,它允许绘制一个图形,连续提取每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,
[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.
我的文件的前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.
如果有人能帮助我,我将不胜感激。您提到这是一个大的文本文件,但是加载整个文本文件,然后简单地每三行采样一次是否会有问题,例如,如下所示:
[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
我终于找到了办法。我在这里粘贴代码,允许我从文本文件中每隔三行绘制一个图形
[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