フィルターのクリア

Trouble deleting plots with circle animation

3 ビュー (過去 30 日間)
Job
Job 2023 年 9 月 18 日
編集済み: Adam Danz 2023 年 9 月 19 日
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)
ans =
Line with properties: Color: [0.3010 0.7450 0.9330] LineStyle: '-' LineWidth: 4 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [9 8.9822 8.9290 8.8406 8.7172 8.5595 8.3680 8.1434 7.8868 7.5990 7.2812 6.9346 6.5607 6.1609 5.7368 5.2901 4.8224 4.3358 3.8320 3.3131 2.7812 2.2382 1.6864 … ] (1×101 double) YData: [0 0.5651 1.1280 1.6864 2.2382 2.7812 3.3131 3.8320 4.3358 4.8224 5.2901 5.7368 6.1609 6.5607 6.9346 7.2812 7.5990 7.8868 8.1434 8.3680 8.5595 8.7172 8.8406 … ] (1×101 double) Use GET to show all properties
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

採用された回答

Adam Danz
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 件のコメント
Job
Job 2023 年 9 月 19 日
Thanks, this solution seems more elegant indeed. However, my axis are still not behaving how they should. The axis rescales for every value in M, so that not the circle changes in size, but rather the axis. This happen even though I initialised the axis as [-15 15 -15 15], and set the axis to square. (This only happens with the second solution though, confusing me even more)
Adam Danz
Adam Danz 2023 年 9 月 19 日
You have to hold the axes prior to running the animation.
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 ExchangeLabels and Annotations についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by