MATLAB Answers

using subplot(m,n,p,ax) in a loop

26 views (last 30 days)
Payal Bal
Payal Bal on 11 Jun 2015
Commented: Ingrid on 3 Feb 2017
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.

Accepted Answer

Ingrid
Ingrid on 11 Jun 2015
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)
  2 Comments
Ingrid
Ingrid on 3 Feb 2017
when you type
doc image
you can learn that what you want is to not generate a new plot (A high-level function that calls newplot to determine where to draw the graphics objects and sets the following axes properties) as you are currently doing, but that you require A low-level function that adds the image to the current axes without calling newplot. The low-level function argument list can contain only property name/property value pairs. So basically you have to change your call to image so that you are plotting to the currently selected axes
hFig = figure;
for i= 1:12
% Subplot 1
figure(1)
hAX(i) = subplot(4,3,i); % i is the counter of the loop for example
xlim (hAX(i),[0 1]);
ylim (hAX(i),[0 100]);
box(hAX(i),'off');
hold(hAX(i),'all')
image('PropertyName',PropertyValue,...) % fill in for your specific case
colorbar;
end

Sign in to comment.

More Answers (1)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh on 11 Jun 2015
Just put figure outside the loop.
If you want to change each axis or maybe axes properties, use xlim ylim in your for loop you can only have
for i = 1:N
subplot(1,N);
plot(data);
xlim([-x,x]);
ylim([-y,y]);
end
something like this
  1 Comment
Payal Bal
Payal Bal on 13 Jun 2015
Thanks. Yes was doing it all wrong.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!