For loop and subplot
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2019-8-15
subplot(6,2,2*i-1)
subplot(6,2,2*i)
3 个评论
Walter Roberson
2019-8-15
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
2019-8-15
Use this simple scheme i created for you
Numbers in mesh indicates number of subplot
subplot(3,2,i)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!