Production and preservation of MATLAB animation

Matlab has three methods to create animations:

①Create animation in the form of particle motion trajectory

theta=0:0.001:2*pi;
%定义圆的半径
r=10;
%生成圆上各点的横纵坐标
x=r*cos(theta);
y=r*sin(theta);
comet(x,y);

The generated animation effect is as follows (one of the steps)

The final result is as follows

 

comet(x,y) Comet plot   showing  y pairs  .x

The use of comet3 is similar to that of comet;

Continuously grab a series of pictures and save them during the drawing process, and then play these pictures continuously in the background. This method is called "movie mode"

Using this mode to generate animation requires the following two steps

a. Call the getframe function to capture the interface and save it as an image

b. Call the movie function to continuously play the saved images

clc;clear;
figure(1);
x=0:pi/50:2*pi;
y=sin(x);
h=plot(x,y);
%保存坐标值,使得所有的帧都显示在同一个坐标轴中
axesValue=axis;
%创建动画
for ii=1:10
    %在坐标轴中绘制曲线
    for jj=1:length(x)*ii/10
        set(h,'xdata',x(1:jj),'ydata',y(1:jj),'color','r');
        axis(axesValue);
    end
    %抓取界面图像,保存到矩阵A
        A(ii)=getframe;
end
%连续播放矩阵A中的图像两次
movie(A,2);

 

 

③Erase mode

 Using wipe mode to animate requires the following steps

a. Generate a graphic object, set its EraseMode property to xor;

b. Change the coordinate value of the object (XData, YData, ZData attribute value) in the loop, and redraw the graph

Call the drawnow command every cycle to refresh the interface

figure(1);
x=0:pi/50:2*pi;
y=sin(x);
plot(x,y);
%产生一个红点在曲线上移动
h=line(0,0,'color','r','marker','.','markersize',40,'erasemode','xor');
%取得坐标轴的取值范围
axesValue=axis;
%创建动画,在曲线上移动红点
for jj=1:length(x)
    set(h,'xdata',x(jj),'ydata',y(jj),'color','r');
    %设置坐标轴的范围,以使所有图形的大小相同
    axis(axesValue);
    pause(0.2);
    drawnow;
end

 

 

Guess you like

Origin blog.csdn.net/yangSHU21/article/details/131343921