2014-01-17 78 views
1

我有一個大的文本文件,其中有兩列,其中的值用逗號分隔。我正在嘗試創建一個簡單的程序,該程序允許連續繪製每3行所提取數據的圖形,直至到達文件末尾。在MATLAB中:如何從文本文件中每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'來在圖上繪製先前生成的輸出。我知道我需要一些循環whiles來重複這個過程,並指出文件的末尾等等。但是我正在努力尋找這樣做的方法:-(。

我只管理繪製一個圖第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

你提到它是一個很大的文本文件,但是加載整個文本文件然後簡單地對每行第三行進行採樣,例如如下所示:

[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 
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 
相關問題