Use the 'imwrite' function to create an animated GIF. An animated GIF contains a series of images all combined into one file. For this example,
1. Draw a series of plots for the function y = x^n for different values of n
2. Capture them as images
3. Write them into a GIF file
h = figure;
axis tight manual
filename = 'testAnimated.gif';
for n = 1:0.5:5
x = 0:0.01:1;
y = x.^n;
plot(x,y)
drawnow
frame = getframe(h);
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
For a more detailed description on creating an animated GIF File, see the example “Write Animated GIF” on the 'imwrite' function reference page:
10 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_470691
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_470691
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_470728
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_470728
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_473780
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_473780
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_622918
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_622918
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_714946
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_714946
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1052311
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1052311
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1052696
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1052696
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1054956
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1054956
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1056546
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1056546
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1056641
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab#comment_1056641
Sign in to comment.