フィルターのクリア

How do I plot a figure and animate overlaying plots in a separate figure?

37 ビュー (過去 30 日間)
Mauricio Fernández
Mauricio Fernández 2019 年 4 月 29 日
回答済み: Hussein 2023 年 7 月 8 日
I am trying to understand how to plot a figure and animate on a separate one other stuff. The following piece of code
clear all;
close all;
clc;
% data
x = linspace(0,3,100);
f = sin(x);
% plot 1
figure;
plot(x,x);
% plot 2
figure;
plot(x,3*f);
% plot 3
figure;
for i=1:0.1:5
scatter(x,f);
hold on;
plot(x,i*f);
hold off;
drawnow;
end
creates two figures (plot 1 and 2) and then I wanted to animate something on the third figure, where each frame is to be drawn individually based on current plot commands or user-defined plot functions (i.e., I really want to plot first with scatter and on top of it plot the amplified sinus, I dont want to use plot(x,f,x,i*f)). My problem is that the animation sometimes is placed on figure 3 and sometimes on figures 1 or 2. I dont understand why and how I can solve this. Can you help me out? Thank you!

採用された回答

Mauricio Fernández
Mauricio Fernández 2019 年 4 月 29 日
One solution is just to explicitly define the function handle 'fh' and call it before plotting (no need for set(...) or stuff like that)
clear all;
close all;
clc;
% data
x = linspace(0,3,100);
f = sin(x);
% plot 1
figure;
plot(x,x);
% plot 2
figure;
plot(x,3*f);
% plot 3
fh = figure;
for i=1:0.1:5
figure(fh);
scatter(x,f);
hold on;
plot(x,i*f);
hold off;
axis([0,3,0,5]);
drawnow;
end
For a smoother animation, the axis has been set constant for all plots in the for loop.

その他の回答 (2 件)

KSSV
KSSV 2019 年 4 月 29 日
  1 件のコメント
Mauricio Fernández
Mauricio Fernández 2019 年 4 月 29 日
Thanks for the link, but there it is not clear what would solve the current problem. By that I mean if defining figure handles or using set() was the thing to do. I gave a separate answer explicitly defining a function handle and calling it, which seems to be easier.

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


Hussein
Hussein 2023 年 7 月 8 日
clc clear all close all figure Z = peaks; surf(Z) axis tight set(gca,'nextplot','replacechildren','visible','off') f = getframe; [im,map] = rgb2ind(f.cdata,256,'nodither'); im(1,1,1,20) = 0; for k = 1:20 surf(cos(2*pi*k/20)*Z,Z) f = getframe; im(:,:,1,k) = rgb2ind(f.cdata,map,'nodither'); end imwrite(im,map,'DancingPeaks.gif','DelayTime',0.1,'LoopCount',inf) %g443800

カテゴリ

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