MATLAB: Using subplot(m,n,p,ax) in a loop

axesfor loopsubplot

Hi, If I have this plot
x = linspace(1,10);
y = sin(x);
plot(x,y)
title('Sine Plot')
And want to assign it to a subplot with the same axes, I will do
ax1 = gca;
subplot(2,1,1,ax1)
But now if I want a new plot at subplot(2,1,2) with the same axes, I can’t do
ax1 = gca;
subplot(2,1,2,ax1)
because it deletes the previous plot. How can I keep both plots?
Cheers, p.

Best Answer

you cannot define an axes handle in a subplot command because the subplot command generates an axes
You should just write
h1 = subplot(2,1,1)
plot(h1,x1,y1)
h2 = subplot(2,1,2)
plot(h2,x2,y2)