Multi Subplot for loop

조회 수: 17 (최근 30일)
Eric
Eric 2014년 6월 12일
댓글: Geoff Hayes 2018년 4월 23일
Hi, I want to work with two subplots to record data from each iteration of a loop but am not sure how to do this. Code is something like this as of now:
for i = 2:2:12
[C,L,p,S] = func1(n,i)
subplot1 = subplot(2,3,i/2)
semilogx(p,C,p,L)
subplot2 = subplot(2,3,i/2)
semilogx(p,S)
end
Please let me know if there is an easy way to switch between the two in each loop, as right now C,L, and S are shown on the same graphs but I want C and L together with S on a separate subplot. The reason I want to do this is because the range of C and L is 0 to 1, and the range of S is 0 to 40. Please let me know if you need anything further clarified.
  댓글 수: 2
Ramesh Bala
Ramesh Bala 2018년 4월 22일
편집: Geoff Hayes 2018년 4월 23일

I have a question? Unable to plot the matrix values separately The subplot should plot 6,11,16,21,26 separately But its plotting only 26...any idea

for i=6:5:26
%     figure(i)
    for j=1:1:5
        subplot(5,1,j);
        plot(s.data(1,i).wektor_czasu(1,1:10761),s.data(1,i).pomiar(4,1:10761));
    end
end
Geoff Hayes
Geoff Hayes 2018년 4월 23일

Kaleesh - you need to use hold to retain the current plot when adding a new one. Probably something like

 for i=6:5:26
    hold(subplot(5,1,j), 'on');
    for j=1:1:5
        subplot(5,1,j);
        plot(s.data(1,i).wektor_czasu(1,1:10761),s.data(1,i).pomiar(4,1:10761));
    end
 end

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

답변 (3개)

Geoff Hayes
Geoff Hayes 2014년 6월 12일
The above code keeps referring to the same subplot on each iteration since it is using i/2 for each i in either subplot1 = subplot(2,3,i/2) or subplot2 = subplot(2,3,i/2). A different grid position is needed for each subplot.
There are six iterations of the for loop, so presumably there will be 6 pairs of subplots (so 12 subplots in total). Is this correct? The above code indicates that there will be six iterations (due to the 2:2*12) but then a 2x3 is defined using subplot (for only 6 subplots) hence the confusion. If we assume the the six pairs then we can do the following
for i = 2:2:12
[C,L,p,S] = func1(n,i);
subplot1 = subplot(6,2,i-1);
semilogx(p,C,p,L);
subplot2 = subplot(6,2,i);
semilogx(p,S);
end
From http://www.mathworks.com/help/matlab/ref/subplot.html, MATLAB numbers its grids by row, such that the first grid is the first column of the first row, the second grid is the second column of the first row, and so on. So the two grids in the first row are accessed by 1 and 2, the two in the second row are accessed by 3 and 4, etc.
Since i is a multiple of 2, then on the first iteration we want to update subplots/grids 1 and 2 (so i-1 and i for i==2), on the second iteration we want to update subplots/grids 3 and 4 (again, i-1 and i for i==4), etc. That is why the above uses subplot(6,2,i-1) and subplot(6,2,i).
Try the above and see what happens!

dpb
dpb 2014년 6월 12일
How about plotyy instead?
But, since suplots are numbered from L to R by row, they're
1 2 3
4 5 6
in your window.
for i=2:2:12
[C,L,p,S] = func1(n,i)
subplot1 = subplot(2,3,i/2);
semilogx(p,C,p,L)
subplot2 = subplot(2,3,i/2+3);
semilogx(p,S)
end

Eric
Eric 2014년 6월 13일
hey guys, thanks for all the helpful suggestions. I ended up figuring it out exactly as I wanted to; here's how I personally did it:
for i = 2:4
figure(1)
subplot(1,3,i-1)
semilogx(....
....
figure(2)
subplot(1,3,i-1)
semilogx(different plot data...)
...
end
  댓글 수: 2
José-Luis
José-Luis 2014년 6월 13일
Please accept an answer if it solved your problem
Image Analyst
Image Analyst 2014년 6월 14일
Eric, you can also vote for them as well to give the people reputation points.

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

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by