我想画一个圆圈并在Matlab情节图中移动它。 我正在使用
viscircles([8.1, 8.5], 1);
画圆圈。如何再次调用它来绘制一个新圆圈并删除原始圆圈?还有一种方法可以使用
drawnow
这样做的功能?
答案 0 :(得分:4)
不是删除和重绘,只需在X和Y数据中引入一些常量来移动圆圈。
%%%%Borrowing some code from irreducible's answer%%%%
xc=1; yc=2; r=3;
th = 0:pi/50:2*pi;
x = r * cos(th) + xc;
y = r * sin(th) + yc;
h = plot(x, y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axis([-20 20 -20 20]); %Fixing axis limits
for k=1:20 %A loop to visualise new shifted circles
h.XData = x + randi([-10 10],1); %Adding a constant in the x data
h.YData = y + randi([-10 10],1); %Adding a constant in the y data
pause(0.5); %Pausing for some time just for convenient visualisation
end
答案 1 :(得分:2)
一种可能性是创建自己的圆形函数,返回绘图句柄:
function h = my_circle(xc,yc,r)
% xc x-center of circle
% yc y-center of circle
% r radius
th = 0:pi/50:2*pi;
x = r * cos(th) + xc;
y = r * sin(th) + yc;
hold on
h = plot(x, y);
hold off;
有了这个,你可以绘制你的圆圈
h = my_circle(1,2,3);
如果您不再需要它,请将其删除:
delete(h)
之后你可以绘制一个新的:
h2 = my_circle(1,2,4);