Clearing the last plotted image in a for loop.

31 visualizaciones (últimos 30 días)
Jakob
Jakob el 4 de Oct. de 2023
Comentada: Jakob el 5 de Oct. de 2023
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

Respuesta aceptada

MarKf
MarKf el 4 de Oct. de 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 comentario
Jakob
Jakob el 5 de Oct. de 2023
Thank you! Naming the plot and using the delete() function worked wonders!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by