How can I delete previous circles while circle is moving ?
이전 댓글 표시
I have a function called drawCircle include a small circle and a line to show rotate of circle.
function drawCircle(xCenter,yCenter,theta);
%Circle
hold on
ang = 0 : 0.1 : 2*pi;
radius = 5;
x = radius * cos(ang) + xCenter;
y = radius * sin(ang) + yCenter;
h1=plot(x,y,'b','LineWidth',4);
%Line
theta=theta*pi/180;
u = radius* cos(theta);
v = radius * sin(theta);
h2=quiver(xCenter,yCenter,u,v,'b','LineWidth',3);
end
Also, I have a script file called moveCircle include a bigger circle.In moveCircle,I must call drawRobot and move the small circle on bigger circle . The problem is; while small circle moving, the previous small circles are not deleted. How can I delete them ?
%moveCircle
hold on
axis([-50,50,-50,50])
xCenter = 5; yCenter = 5;
ang = 0 : 0.01 : 2*pi;
radius = 20;
x = radius * cos(ang) + xCenter;
y = radius * sin(ang) + yCenter;
plot(x,y);
for i=1:length(x)
drawRobot(x(i),y(i),theta);
M(i)= getframe(gcf);
end
답변 (1개)
Mike Garrity
2015년 10월 23일
Ah, good question! You've got several options here.
- There's the cla function which will "clear the axes". This will get rid of EVERYTHING in the axes.
- Those handles (h1, h2) that are returned by functions like plot and quiver have a delete function . If you hold on to those handles, you can get rid of them by calling delete.
- Another option, which can be good when you're worried about performance, is to modify the existing circles instead of deleting and recreating them. Again, to do this, you would need to hold on to those handles. Then you can call the set function like this:
set(h1,'XData',x,'YData',y)
- Another efficient option is to use the hgtransform object , which I described in this blog post.
There are probably a couple of other ways to do it too, but one of those will probably get you "moving".
카테고리
도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!