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!