Trouble deleting plots with circle animation
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello everyone,
I want to make a smooth animation of a circle that grows and shrinks according to an (eventually large) vector of radiuses. I have tried to make a function for this, shown below. To display all circles with different radiuses in one figure is no problem, but I can’t figure out how to delete the previous circle. I have tried the delete(h) command in a couple different places, but that didn’t help. If I remove the hold on and hold off command, than only the current circle is plotted, but the axis jumps all over the place.
If you have any insights, let me know!
M = [3 4 6 5 8 9];
axis([-15 15 -15 15])
axis square
circle(M)
function h = circle(M)
for i = 1:length(M)
hold on
a = 0:pi/50:2*pi;
x = M(i) * cos(a);
y = M(i) * sin(a);
h = plot(x, y, "LineWidth", 4);
pause(0.1)
hold off
end
end
댓글 수: 0
채택된 답변
Adam Danz
2023년 9월 18일
편집: Adam Danz
2023년 9월 19일
Based on the code, I think you want to delete the previous circle here (marked by a comment below).
M = [3 4 6 5 8 9];
axis([-15 15 -15 15])
axis square
hold on
circle(M);
hold off
function h = circle(M)
for i = 1:length(M)
a = 0:pi/50:2*pi;
x = M(i) * cos(a);
y = M(i) * sin(a);
h = plot(x, y, "LineWidth", 4);
pause(0.1)
delete(h) %<---- Try this
end
end
However, this approach would be more efficient.
Instead of deleting and replacing the circle, you can update it's x and y values. Unlike the previous version, this will not change the color of the circle but you could update that as well.
M = [3 4 6 5 8 9];
axis([-15 15 -15 15])
axis square
circle(M);
function h = circle(M)
h = plot(nan,nan,'LineWidth',4); % <- initialize the graphics object
for i = 1:length(M)
a = 0:pi/50:2*pi;
x = M(i) * cos(a);
y = M(i) * sin(a);
set(h,'XData',x,'YData',y) % <---- update x,y
pause(0.1)
end
end
댓글 수: 2
Adam Danz
2023년 9월 19일
axis([-15 15 -15 15])
axis square
hold on
box on
a = 0:pi/50:2*pi;
plot(cos(a),sin(a))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Labels and Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!