For loop and subplot
1 view (last 30 days)
Show older comments
I have a for loop and in each loop I plot to figures. I want each pair of figure to be plotted next to each other (left and right).
I mean for i=1 I have two plots. I want one to be on the left and one on the right and so on.
I tried subplot(6,2,i); and subplot(6,2,i+1); in each loop but did not work. i=1 would plot on the first row left. i=2 would plot on the 1st row right but I wanted to plot on the second row left.
0 Comments
Answers (1)
Walter Roberson
on 15 Aug 2019
subplot(6,2,2*i-1)
subplot(6,2,2*i)
3 Comments
Walter Roberson
on 15 Aug 2019
plots_down = 6; plots_across = 2;
subplot( plots_down, plots_across, sub2ind([plots_across, plots_down], column_of_plot, row_of_plot))
For example,
%notice the 6, 2 in one place but the 2, 6 in the other case
subplot(6, 2, sub2ind([2, 6], 1, i)) %left
subplot(6, 2, sub2ind([2, 6], 2, i)) %right
This can be done more directly by using the formula for sub2ind directly, as
subplot( plots_down, plots_across, (row_of_plot-1)*plots_across + column_of_plot)
which for 2 plots across would be
subplot( plots_down, 2, (i-1) * 2 + 1) %left
subplot( plots_down, 2, (i-1) * 2 + 2) %right
and in your particular case that would optimize as
subplot(6, 2, 2*i - 1) %left
subplot(6, 2, 2*i) %right
so this is a systematic way.
darova
on 15 Aug 2019
Use this simple scheme i created for you
Numbers in mesh indicates number of subplot
subplot(3,2,i)
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!