Effacer les filtres
Effacer les filtres

Multi Subplot for loop

18 vues (au cours des 30 derniers jours)
Eric
Eric le 12 Juin 2014
Commenté : Geoff Hayes le 23 Avr 2018
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 commentaires
Ramesh Bala
Ramesh Bala le 22 Avr 2018
Modifié(e) : Geoff Hayes le 23 Avr 2018

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 le 23 Avr 2018

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

Connectez-vous pour commenter.

Réponses (3)

Geoff Hayes
Geoff Hayes le 12 Juin 2014
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 le 12 Juin 2014
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 le 13 Juin 2014
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 commentaires
José-Luis
José-Luis le 13 Juin 2014
Please accept an answer if it solved your problem
Image Analyst
Image Analyst le 14 Juin 2014
Eric, you can also vote for them as well to give the people reputation points.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Two y-axis dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by