How can I delete previous circles while circle is moving ?

7 visualizaciones (últimos 30 días)
Ertugrul CAKIR
Ertugrul CAKIR el 23 de Oct. de 2015
Respondida: Mike Garrity el 23 de Oct. de 2015
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

Respuestas (1)

Mike Garrity
Mike Garrity el 23 de Oct. de 2015
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)
There are probably a couple of other ways to do it too, but one of those will probably get you "moving".

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by