MATLAB制作简单动画的方法

Matlab实现动画形式主要有两种,一种是电影形式:将所有动画预存再像电影般地播放;另一种形式是对象形式:所有点所有曲线曲面均可以作为一个对象,不断抹去旧曲线,产生新曲线

1.先介绍一下第一种方式,电影法,也是常用的方法:

%制作动画(这种方法很费内存)
plot(1,ut(1),'b');
axis([0 1000 3.3 4.2])%定义x轴(从0到100)和y轴的范围(从3.3到4.2)
theAxes=axis;
M=moviein(1000); %前面要有plot帮助moviein初始化
for k=1:1000
% plot(k,ut(k),'b');hold on;
% plot(k,Usim(k),'r'); %这会实时在屏幕上绘制曲线动画
%set(h,'xdata',k,'ydata',ut(k));
axis(theAxes)%定义x轴(从0到100)和y轴的范围(从3.3到4.2)
M(:,k)=getframe; %抓取图形作为电影的画面
end
movie(M,1,30);  %以每秒30帧的速度播放1次

%2.进阶(用句柄画多个对象,占内存很小)
X_MIN=1;
X_MAX=14000; %指定显示点的个数
Y_MIN=3.0;
Y_MAX=4.2;
% h1=line('color',[1,0,0],'marker','+','markersize',3);
% h2=line('color',[0,0,1],'marker','o','markersize',3); %none保留旧的曲线的点,不做任何处理
% set(h1,'EraseMode','none');
% set(h2,'EraseMode','none');

a1=line('Linestyle','-.','marker','+','Color',[1 0 0],'markersize',2,'EraseMode','none');
a2=line('Linestyle','-','marker','o','Color',[0 0 1],'markersize',2,'EraseMode','none');
a3=line('marker','s','Color',[0 1 0],'markersize',2,'EraseMode','none');
axis([X_MIN X_MAX Y_MIN Y_MAX]);
grid on
hleg1= legend(gca, [a1 a2 a3], 'Measured', 'Model Predict1','Model Predict2', 0); %为两条曲线创建图例
set(hleg1,'Box','on','position',[0.6,0.75,0.2,0.1]);
set(get(gca,'Xlabel'),'String','Time(s)','FontName','Times New Roman','FontSize',10,...
'FontWeight','bold','FontAngle','normal');%设置坐标轴刻度字体名称,大小
set(get(gca,'Ylabel'),'String','Voltage(V)','FontName','Times New Roman','FontSize',10,...
'FontWeight','bold','FontAngle','normal');%设置坐标轴刻度字体名称,大小

for t=10:-1:1  %倒计时时间
    pause(1);
    fprintf('%d\r\n',t);
end
fprintf('ready\r\n');
for i=X_MIN:X_MAX
    set(a1,'xdata',i,'ydata',ut(i));
    set(a2,'xdata',i,'ydata',Usim(i));   
    set(a3,'xdata',i,'ydata',Usim2(i));
    drawnow;
    %pause(01)
end
fprintf('finished!')

%%
%3.再次进阶,使用plot将上述动画保存成逐帧打印的(保存为AVI文件)
aviobj=VideoWriter('compare.avi');%新建叫example.avi的文件
open(aviobj); %打开example.avi的文件
X_MAX=1400; %指定显示点的个数
% pause(5);
% fprintf('ready\r\n');
for i=1:X_MAX
    plot(i,ut(i),'marker','+','markersize',3,'color',[1,0,0]);
    hold on;
    plot(i,Usim(i),'marker','o','markersize',3,'color',[0,0,1]);
    axis([0 X_MAX 3.0 4.2]);
    grid on
    drawnow;
    currFrame = getframe;
    writeVideo(aviobj,currFrame);
    %pause(01)
end
close(aviobj); %关闭
fprintf('finished!\r\n');

%%
%保存为GIF文件
filename='Curve_Compare4.gif';
X_MAX=1000; %指定显示点的个数
plot(1,ut(1),'marker','+','markersize',3,'color',[1,0,0]);
hold on;
plot(1,Usim(1),'marker','o','markersize',3,'color',[0,0,1]);   
set(get(gca,'Xlabel'),'String','Time(s)','FontName','Times New Roman','FontSize',10,...
    'FontWeight','bold','FontAngle','normal');%设置坐标轴刻度字体名称,大小
set(get(gca,'Ylabel'),'String','Voltage(V)','FontName','Times New Roman','FontSize',10,...
    'FontWeight','bold','FontAngle','normal');%设置坐标轴刻度字体名称,大小
%set(gca,'position',[0.1 0.1 1 1]);
for i=1:X_MAX
    plot(i,ut(i),'marker','+','markersize',3,'color',[1,0,0]);
    hold on;
    plot(i,Usim(i),'marker','o','markersize',3,'color',[0,0,1]);
    axis([0 X_MAX 3.0 4.2]);
    grid on
    drawnow;
    f=getframe(gcf);  
    imind=frame2im(f);
    [imind,cm] = rgb2ind(imind,256);
    if i == 1
        imwrite(imind,cm,filename,'gif', 'Loopcount',1,'DelayTime',0); %0用最快的速度
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',0);
    end
    %pause(01)
end
fprintf('finished!\r\n');
close(gcf)
%%
%转成avi
X_MAX=100; %指定显示点的个数
plot(1,ut(1),'marker','+','markersize',3,'color',[1,0,0]);
hold on;
plot(1,Usim(1),'marker','o','markersize',3,'color',[0,0,1]);   
set(get(gca,'Xlabel'),'String','Time(s)','FontName','Times New Roman','FontSize',10,...
    'FontWeight','bold','FontAngle','normal');%设置坐标轴刻度字体名称,大小
set(get(gca,'Ylabel'),'String','Voltage(V)','FontName','Times New Roman','FontSize',10,...
    'FontWeight','bold','FontAngle','normal');%设置坐标轴刻度字体名称,大小
%set(gca,'position',[0.1 0.1 1 1]);
MOV=moviein(X_MAX);
for i=1:X_MAX
    plot(i,ut(i),'marker','+','markersize',3,'color',[1,0,0]);
    hold on;
    plot(i,Usim(i),'marker','o','markersize',3,'color',[0,0,1]);
    axis([0 X_MAX 3.0 4.2]);
    grid on
    drawnow;  
    MOV(i)=getframe(gcf);
    %pause(01)
end
movie2avi(MOV, 'mytest.avi','compression','None','FPS',60);
fprintf('finished!\r\n');
close(gcf)

%%
%4.这个是摘抄的例子实现了坐标轴移动+avi视频
aviobj=VideoWriter('example3.avi');%新建叫example.avi的文件
open(aviobj); %打开example.avi的文件
t=[0]
m=sin(t)
p = plot(t,m,'EraseMode','background','MarkerSize',5);
x=-1.5*pi;
axis([x x+2*pi -1.5 1.5]);
grid on;
for i=1:100
    t=[t 0.1*i];
    m=[m sin(0.1*i)];
    set(p,'XData',t,'YData',m)
    drawnow
    x=x+0.1;
    axis([x x+2*pi -1.5 1.5]);
    pause(0.01); %最快速度是10帧/s,即使注释掉这句
    currFrame = getframe;
    writeVideo(aviobj,currFrame);
end
close(aviobj); %关闭
fprintf('finished!')

%%
%5.新的技术animatedline出错??????animatedline是2014b引入的函数
a1 = animatedline('Color',[0 .7 .7]);
a2 = animatedline('Color',[0 .5 .5]);

axis([0 20 -1 1])
x = linspace(0,20,10000);
for k = 1:length(x);
    % first line
    xk = x(k);
    ysin = sin(xk);
    addpoints(a1,xk,ysin);

    % second line
    ycos = cos(xk);
    addpoints(a2,xk,ycos);

    % update screen
    drawnow limitrate
end

 

 

你可能感兴趣的:(Matlab/Simulink)