# Using a for loop to plot multiple sets of data

조회 수: 7 (최근 30일)
Kacey Lange 2022년 7월 27일
댓글: David Hill 2022년 7월 27일
I am trying to use a for loop to plot multiple sets of data. My data matrix (fTSM) is 42x1 where I have six sets of data (corresponding to 6 weeks) from seven stations. The data is organized by the weeks the data was sampled, in order of the stations that are being sampled each week. I want to plot data each stations data in a time series, where the x-axis are the sampling weeks 1-6 and there will be seven lines in the graph corresponding to the seven stations. The for loop I have written is:
for k = 1:7:42
PC = fTSM(k);
PM = fTSM(k+1);
TG = fTSM(k+2);
SJ = fTSM(k+3);
HP = fTSM(k+4);
BL = fTSM(k+5);
BB = fTSM(k+6);
x = [1:7]';
fprintf('%d: %s vs %s\n',k,PC,PM,TG,SJ,HP,BL,BB);
h1=semilogy(x,[PC;PM;TG;SJ;HP;BL;BB],'LineWidth',1.5);
ylim([0 110])
xlim([1 7])
hold on
set(gca,'FontSize',20)
legend({'PC','PM','TG','SJ','HP','BL','BB'},'location','northeast','FontSize',6);
set(h1,'linewidth',1.5);
xlabel('Sampling week','FontSize',20);
ylabel('VSF (m^{-1} sr^{-1})','FontSize',20);
end
The outcome of this that there are six lines with seven weeks of data where the data is in order, instead of going to the next week of data. For example PC consists of fTSM(1:7) instead of fTSM(1,8,15,22,29,36) and PM consists of fTSM(8:14) instead of fTSM(2,9,16,23,30,37) and so on. This leaves out station BB in the graph.
##### 댓글 수: 2없음 표시없음 숨기기
Dyuman Joshi 2022년 7월 27일
There are only 6 sets of data and you get 6 lines in the plot as well.
1:7 , 8:14 , 15:21 , 22:28 , 29:35 , 36:42
And since your legend has 7 elements in it, it doesn't show the extra one(s) here.
Kacey Lange 2022년 7월 27일
No there are six sets of data from seven stations.
1st station is fTSM(1,8,15,22,29,36)
2nd station is fTSM(2,9,16,23,30,37)
3rd station is fTSM(3,10,17,24,31,38)
4th station is fTSM(4,11,18,25,32,39)
5th station is fTSM(5,12,19,26,33,40)
6th station is fTSM(6,13,20,27,34,41)
7th station is fTSM(7,14,21,28,35,42)
I want all the stations to be separate lines with 6 data points in each, corresponding to the 6 weeks of data collection.

댓글을 달려면 로그인하십시오.

### 채택된 답변

David Hill 2022년 7월 27일
fTSM=reshape(fTSM,7,[])';
h1=semilogy(fTSM);
ylim([0 110])
xlim([1 7])
set(gca,'FontSize',20)
legend({'PC','PM','TG','SJ','HP','BL','BB'},'location','northeast','FontSize',6);
set(h1,'linewidth',1.5);
xlabel('Sampling week','FontSize',20);
ylabel('VSF (m^{-1} sr^{-1})','FontSize',20);
##### 댓글 수: 2없음 표시없음 숨기기
Kacey Lange 2022년 7월 27일
This worked. So I don't need a for loop?
David Hill 2022년 7월 27일
Nope

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by