MATLAB: Problem using the subplot command

graphsmultipleplotsubplotsubplots

Hello, I want to plot 3 graphs in a single figure. I want to use the subplot command. However, I am unable to plot the graph vertically.
eg:
x = 0:5;
y = sin(x);
z = log(y);
subplot(2,2,1:3), plot(x,y)
subplot(2,2,2), plot(y,z)
subplot(2,2,4), plot(x,z)
This code does not work properly.
However, if I try to plot the graph horizontally, it works very well
eg:
x = 0:5;
y = sin(x);
z = log(y);
subplot(2,2,1:2), plot(x,y)
subplot(2,2,3), plot(y,z)
subplot(2,2,4), plot(x,z)
why is this so ? How can I plot vertically ?
Harshal

Best Answer

When a later plot overlaps an earlier plot, it won't show the earlier plot. When you did 1:3 you are really saying [1,2,3], which is an L-shape which you can't plot anyway.
1 2
3 4
Even if you could, when you come along and plot 2, it would blow it away, but like I said, you can't even plot that shape. What you want is just 1 and 3, not 1, 2, and 3. So you want [1 3], not 1:3.
subplot(2, 2, [1 3]);
Or you can do [1 2], [3 4], or in general any combinations that make a rectangle. You can even mix them for example:
subplot(2,2,1); % Plot upper left
subplot(2,2,2); % Plot upper right.
subplot(2, 3, 5); % Plot lower middle of a 2 row by 3 column layout.
The last case will assume a layout like this:
1 2 3
4 5 6
and the lower middle place is #5. When you do that, the first two plots will stay put according to the 2 by 2 layout - they will not move to the 1 and 3 locations of a 2 by 3 layout, which is a good thing - that's how you want it to work.