how to use subplot command to display multiple images in single figure window

41 views (last 30 days)
i found the code subplot(2,2,1).i couldn't understand the values passed in the subplot function.please explain this

Answers (1)

Walter Roberson
Walter Roberson on 8 Feb 2018
The general form is
subplot(M, N, K)
This says that you want to divide up the figure into M rows and N columns of axes, and that you want to select the K'th of them. The numbering is along the rows first, top left to top right, and headed downwards from there
Thus, subplot(2,2,1) says that you want 2 rows of axes, each with 2 columns, and that you want the first of those, which is the top left of them. (2,2,2) would mean you want the second of them, which is the top right. (2,2,3) would mean you want the third of them, which is the bottom left. (2,2,4) would mean that you want the fourth of them, which is the bottom right.
Calling subplot like this only creates the axes for the one location you specify, so subplot(2,2,1) does not go ahead and create 4 axes: instead it calculates as if you plan to put in 2 rows and 2 columns. You do not need fill them all up. It would be valid, for example, the
subplot(2,2,1); subplot(2,2,2); subplot(2,2,4);
which would create the top left, top right, and bottom right, without creating the bottom left axes because you did not call subplot(2,2,3)
subplot allows some more complex cases than I have discussed above, allowing you to construct axes that span multiple slots. For example,
subplot(2,2,[1 3]); subplot(2,2,2); subplot(2,2,4)
would create a single axes on the left side that was 2 rows high and 1 column wide, together with a 1 x 1 on the upper right and another 1 x 1 on the bottom right
A B
A C

Community Treasure Hunt

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

Start Hunting!