Matlab保存动图gif

保存效果:
Matlab保存动图gif_第1张图片

function MakeGif(filename,i)  
    f = getframe(gcf);  
    imind = frame2im(f);  
    [imind,cm] = rgb2ind(imind,256);  
    if i==1  
        imwrite(imind,cm,filename,'gif',...
                'Loopcount',inf,'DelayTime',0.001);
    else  
        imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',0.001);
    end  
end  

将上述代码保存为函数脚本并命名为MakeGif;调用方法参见下面示例:

close all  
clear  
clc  
Times=10; [x,y]=meshgrid(-2:0.01:2);  
for i=1:Times  
	z(:,:,i)=-(17*x.^2-16*y.*abs(x)+17*y.^2).*i/5;  
end  
figure  
axis off
colormap(jet)
view([0 90]);  
hold on  
for i=1:Times  
	mesh(x,y,z(:,:,i));  
	drawnow;  
	MakeGif('heart-waves.gif',i)  					%注意
	hold on  
end  

hold off

你可能感兴趣的:(matlab)