Clear Filters
Clear Filters

Clearing the last plotted image in a for loop.

24 views (last 30 days)
I want this program (see below), to plot a blue circle travelling in a circular path. The radius of the path is the variable r and the orbital period is the variable T. The problem I'm having is that the previous blue circles aren't deleted. The plot looks like this:
function solsystem(r,T)
bildNr = 0;
for t=linspace(0,10,100)
plot(r.*cos(2/pi-w*t),r.*sin(2/pi-w*t),'bo')
drawPath(r)
plot(0,0,'o','MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor','y')
var(r,T)
figure(1),clf;
bildNr = bildNr +1;
film(bildNr)=getframe;
end
function drawPath(r)
beta = linspace(0, 2*pi);
plot(r.*cos(beta),r.*sin(beta),'k');
axis equal;
end
function var(T)
w = 2*pi./T; %vinkelhastighet%
end
end

Accepted Answer

MarKf
MarKf on 4 Oct 2023
The function provided does not work out of the box (and you haven't boxed it in code format anyway) so you have more than the issue of not having the previous dot disappear.
If that were the only issue you could just capture the object when you plot with a handle and then delete only that afterwards before plotting the next, no need to clear with clf and recreate the figure ( ho = plot(...,'bo'); delete(ho) ).
See below.
%% function solsystem(r,T)
r = 10; T = 5; center = [0,0];
bildNr = 0; w = wvar(T);
plot(center(1),center(2),'o','MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor','y')
axis([center(1)-r,center(1)+r,center(2)-r,center(2)+r].*1.1), axis equal,
hold on
hp = drawPath(r); %should this just draw just the travelled circular path
% (then put back inside loop and get sector)? if it's the whole thing can stay out like yellow center
for t=linspace(0,10,4) %4 just to show
if t, delete(ho), end
ho = plot(r.*cos(2/pi-w*t),r.*sin(2/pi-w*t),'bo');
bildNr = bildNr +1;
film(bildNr)=getframe; %could preallocate
filmdat(:,:,:,bildNr) = film(bildNr).cdata;
end
figure
montage(filmdat) %immovie/implay or the moving above does not work in online answers
function h = drawPath(r)
beta = linspace(0, 2*pi);
h = plot(r.*cos(beta),r.*sin(beta),'k');
end
function w = wvar(T)
w = 2*pi./T; %vinkelhastighet%
end
% end
  1 Comment
Jakob
Jakob on 5 Oct 2023
Thank you! Naming the plot and using the delete() function worked wonders!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!