Clearing the last plotted image in a for loop.
조회 수: 29 (최근 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!