How to animate different plots at the same time?

52 ビュー (過去 30 日間)
Tong Zhao
Tong Zhao 2017 年 7 月 2 日
編集済み: Tong Zhao 2017 年 7 月 3 日
I am wondering if there's a way to animate two different figures (Figure 1 and figure 2 For example) at the same time. I know the addpoints and drawnow command can animate plots, but they seem to be working smooth only in the same figure (subplots animated at the same time is achievable using addpoints or even just using plot, with a for loop to plot one point on each subplot at a time).
But, how do you animate two different figures (that is, in two different program windows) together without having to constantly switch current figure? If you don't switch current figure, none of the addpoints/plot commands would draw data points on the other figure. But if you do switch current figure and switch back frequently in a for loop, you'll see the edge of the figure windows flickering/flashing because of changing window focus, which is annoying. Is there a way to have two different figures do their animation at the same time without switching focus between them constantly?

採用された回答

Geoff Hayes
Geoff Hayes 2017 年 7 月 2 日
Tong - don't switch to the current figure, but rather use the handle to the figure (if needed) or (better) the handle to the axes or (even better) the handle to the graphics object that you want to update. Then you don't have to explicitly set focus to the "current figure" in order to update its axes.
For example,
hFig1 = figure;
hAxes1 = gca;
hPlot1 = plot(NaN,NaN);
hFig2 = figure;
hAxes2 = gca;
hPlot2 = plot(NaN,NaN);
for k=1:40
xdata = get(hPlot1,'XData');
ydata = get(hPlot1,'YData');
set(hPlot1,'XData',[xdata k],'YData',[ydata randi(255,1,1)]);
xdata = get(hPlot2,'XData');
ydata = get(hPlot2,'YData');
set(hPlot2,'XData',[xdata k],'YData',[ydata randi(255,1,1)]);
pause(0.5);
end
  2 件のコメント
Tong Zhao
Tong Zhao 2017 年 7 月 3 日
Hi Geoff, Thank you, your code does solve the major problem. And now I know the basic principle, which is to not change focus between figures, just use the handle to change values.
But what if I want to use the addpoints function and have the trace feature with the addpoints function. The code you have can add new data to the existing graph, but I also don't want the entire data history, but only the most recent 20 points. I think yes you can write your own code to delete one oldest data point each time, then add a new data point after that, but it will be not as simple as the addpoints function which you can just use. Do you have any idea whether I can still use addpoints to update the animation in two different figures? Thank you!
Tong Zhao
Tong Zhao 2017 年 7 月 3 日
編集済み: Tong Zhao 2017 年 7 月 3 日
I found the solution to my problem, thanks to another thread: here is the link
The point is there's a property of addpoints called 'Parent'. What it does it that it will link the curve created by addpoints to a particular 'parent' axes. Then when you call the addpoints command, it will automatically plot only in the specified 'parent' axes. Pretty neat.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by