delete a subplot and renew plot

Hi guys,
i am trying to update my subplot in a for loop, like;
fig1=figure;
for i=1:10
axes1 = axes('Parent',fig1, 'Position',[0.043 0.77 0.43 0.15]); box(axes1,'on'); hold(axes1,'all');
plot(rand(1,100),'Parent',axes1)
end
it works! and for each run i have a new graph. BUT, it has one problem; overlapping subplot box.
what should i do, to avoid this overlapping

5 Comments

What, precisely, do you want? Obviously when you use a fixed 'Position' vector you're going to get an axes object in the same position.
Plus, each loop creates a new axes but overwrites the handle of the previous. So, at the end of the above loop you have 10 active axes but no way w/o "handle-diving" to access any but the last.
Why aren't you just using subplot?
thanks dpb, using subplot, solved my problem as:
fig1=figure;
for i=1:10
subplot1= subplot(4,2,1,'Parent',fig1,'Position',[0.043 0.77 0.43 0.15]);
box(subplot1,'on');hold(subplot1,'all'); cla
plot([1:100],rand(1,100),'Parent',subplot1)
end
for i=1:10
subplot1= subplot(4,2,1,'Parent',fig1,...
That doesn't solve any of your problems; you still have 10 subplot() axes and only one handle.
Again, what are you really trying to do???
Indeed, i don't have any more over-plotting box or axes with above-mentioned code
what i wanted to do was; open a figure+ several subplots at specific positions and update graphs in a for- loop.
Actually, the one problem of multiple handles does go away with subplot for overlapping calls--it has the smarts internally to make the existing the current--but so does just the syntax
subplot(N,M,i)
It would still be much simpler to retain the handle that corresponds to each when creating them (or not save any handles at all) rather than have the same variable refer to a bunch of different axes depending...
As for updating once you've created an axes object, the use of set for [x|y]data is far more efficient as Kevin noted.

Sign in to comment.

Answers (1)

Better yet, update using handles:
figure;
f = 2*pi;
x = linspace(1,100);
y = sin(x/f);
subplot(2,2,1)
plot(x,y);
title('Initial')
subplot(2,2,4);
title('Over Time')
h = plot(x,y);
for ii = 1:1000
newy = sin(1/f*(x - ii/pi));
% update using the handle to the graphics object
set(h,'ydata',newy);
pause(.1)
end

Asked:

on 16 Jun 2014

Commented:

dpb
on 17 Jun 2014

Community Treasure Hunt

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

Start Hunting!