MATLAB Answers

How to make subplot go down the column and not across the row in the first instance?

164 views (last 30 days)
jj
jj on 22 Jun 2013
Edited: Romain on 11 Oct 2016
Hi everyone,
I have a question that I can't seem to figure out. So, I am using subplots to combine two matrices I wish to combine in one diagram. Each matrix has 5 variables that will be plotted. Specifically, I am trying to combine them such that they appear in a 5 rows by 2 columns diagram. I am able to do that. But, when using subplot to combine these 10 figures into the same diagram, Matlab goes across the columns before heading down to the second row when plotting these figures. Is there anyway to make Matlab fill up the first column first before going onto the second column, without specifying the 10 subplots individually (as they are all in 2 matrices)?
Thanks! :)
  1 Comment
Jan
Jan on 22 Jun 2013
SUBPLOT does not combine "figures" to a "diagram", but "diagrams" (better "axes") on a "figure". What does "they are all in two matrices" mean?

Sign in to comment.

Answers (3)

Jan
Jan on 22 Jun 2013
Edited: Jan on 22 Jun 2013
index = reshape(1:10, 2, 5).';
for i = 1:10
subplot(5, 2, index(i));
text(0.5, 0.5, sprintf('%d', i));
end

Matt J
Matt J on 22 Jun 2013
Why not just change the order of the images instead of trying to change the order of the figure plotting. For example, suppose your images exist in a 5x2 cell array. Then just transpose the array,
images=images.';
for i=1:10
subplot(5,2,i)
imshow(images{i})
end

Romain
Romain on 11 Oct 2016
Edited: Romain on 11 Oct 2016
Since I faced the same issue today here an easy solution. I tweaked the subplot script so that it fills the columns first and it works quite well. Go to lines 291-292 and change them as
%row = (nRows - 1) - fix((plotId - 1) / nCols);
%col = rem(plotId - 1, nCols);
% lines to change:
col = (nCols - 1) - ((nCols - 1) - fix((plotId - 1) / nRows));
row = (nRows - 1) - (rem(plotId - 1, nRows));

Community Treasure Hunt

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

Start Hunting!