Clearing the last plotted image in a for loop.
31 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
MarKf
2023-10-4
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!