Animated Gif mit Matlab erstellen
x = 0:0.01:1;
% Grafik erstellen
figure(1)
% Dateiname festlegen
filename = 'AnimatedGif.gif';
% 2 fps
for n = 1:0.5:5
y = x.^n;
plot(x,y)
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
Ergebnis
Beispiel 2
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,'LoopCount',inf)
Ergebnis
<img vspace="5" hspace="5" src="AnimatedGif_01.png" alt="">
Beispiel 3
x = 0:0.01:2*pi;
omega = 0.5;
k = 1;
% Ausgabedatei
outfile = 'sinewave.gif';
for t=1:50
plot(x, sin(omega*t + k*x), 'linewidth', 2, 'color', 'red');
ylim([-1 1]);
xlim([0 2*pi]);
grid on;
% gif utilities
set(gcf,'color','w'); % set figure background to white
drawnow;
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% On the first loop, create the file. In subsequent loops, append.
if t==1
imwrite(imind,cm,outfile,'gif','DelayTime',0,'loopcount',inf);
else
imwrite(imind,cm,outfile,'gif','DelayTime',0,'writemode','append');
end
end
Ergebnis
sinewave.gif Medium:AnimatedGif4.m
Quelle: [1]
→ zurück zum Hauptartikel: Einführung in MATLAB