Single Figure Animation of moving points (without tails)

9 ビュー (過去 30 日間)
Luca Carlino
Luca Carlino 2020 年 6 月 19 日
編集済み: Luca Carlino 2020 年 6 月 24 日
Hello, I am trying to plot an animated figure, where two points move. I need this to be done in a single MATLAB figure. Usually, this is done in a for loop, but this creates multiple figures and, for various reasons, I would prefer to avoid this. I would like to first declare a figure, appropriately prepare it (axes, grid, box, and so on) and then simply draw on it. The hold function doesn't create multiple figures, but it will leave "tails" (previous positions of the moving points). This is a simple working example (single point) of what I am trying to do (but it will leave tails).
P.S. I am aware of the comet function, but it leaves a tail.
%Random Data
x = (1:0.01:2*pi)';
y = sin(x);
%Animated Figure
figure;
hold on;
plot(x(1),y(1),'db');
axis([min(x) max(x) min(y) max(y)]);
xlabel('X', 'Interpreter','Latex');
ylabel('Y', 'Interpreter','Latex');
grid on;
box on;
for k = 2:length(x) %loop
plot(x(k),y(k),'db');
drawnow
end
hold off;
  2 件のコメント
KSSV
KSSV 2020 年 6 月 19 日
Read about set.
Luca Carlino
Luca Carlino 2020 年 6 月 23 日
I'm not sure I am following you.
I think you are suggesting using set to specify the figure properties, which is fine.
Once you remove the hold command, I need to force the plot function (inside the for loop) to draw on the same figure, without (a) creating a new one (default behaviour), and (b) changing the figure properties (axes, etc...), which is specifically what I am unable to do.
Could you please be a little more specific?

サインインしてコメントする。

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 6 月 23 日
Luca - use set to modify the x and y data of the plot. Replace your for loop with the following
hPlot = plot(NaN, NaN, 'db');
for k = 2:length(x) %loop
set(hPlot, 'XData', x(k), 'YData', y(k));
drawnow
end
On each iteration of the loop, we set (change) the x and y data to be the new position.
  1 件のコメント
Luca Carlino
Luca Carlino 2020 年 6 月 24 日
Thank you! This is exactly what I needed.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

タグ

製品


リリース

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by