MATLAB Answers

# How can I create animated GIF images in MATLAB ?

2,949 ビュー (過去 30 日間)
MathWorks Support Team 2009 年 6 月 27 日
コメント済み: Steven Lord 2020 年 10 月 13 日
I would like to know if there is MATLAB functionality to create an animated GIF in MATLAB.

### 採用された回答

MathWorks Support Team 2017 年 6 月 12 日
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 % this ensures that getframe() returns a consistent size
filename = 'testAnimated.gif';
for n = 1:0.5:5
% Draw plot for y = x.^n
x = 0:0.01:1;
y = x.^n;
plot(x,y)
drawnow
% Capture the plot as an image
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
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 件のコメント

Peter Beringer 2020 年 10 月 12 日
I'm trying to make this work with a surface plot and not getting very far. Is someone please available to give me some proper help rather than lazily posting a link to another very vaguely related thread?
Adam Danz 2020 年 10 月 13 日
@Peter Beringer, "Can the GIF then be converted into a *.AVI or *.MOV file within MATLAB?"
You can use gif2avi() from the file exchange to convert from GIF to AVI or MP4 format.
Steven Lord 2020 年 10 月 13 日
Why does it return a picture of a kid when evaluated on its own?
What did you pass into rgb2ind when you called it? Did you call image with no inputs, capture the result using getframe and frame2im, and then call rgb2ind on the result? If so the picture of a kid is not coming from rgb2ind but from image. Steve Eddins explains what's going on in this post on his blog.
As for what the rgb2ind line is doing: getframe returns a struct array. imwrite doesn't know how to accept that struct array as its first (or first two) inputs. frame2im turns that struct array into an image that imwrite could accept, but the image may have more colors in it than the image file format supports so rgb2ind modifies the image to use a colormap with at most 256 colors. It is that image and colormap pair that imwrite writes to the file.
If the bounding box for your surface plot was the same each time you plotted, I would expect this to work. But if the surface plot was rotated or enlarged (so that the area on the screen taken up by each frame would be different) that would be a problem. It would be like printing a report where the first page was on a sheet of letter paper, the second was a 3x5 index card, and the third was a sheet of legal sized paper. That wouldn't make for a very stable stack of reports.

サインインしてコメントする。

### その他の回答 (3 件)

Chad Greene 2017 年 6 月 4 日
Or, for a much simpler option, use the gif function on File Exchange.

#### 0 件のコメント

サインインしてコメントする。

Shashank Rai 2017 年 5 月 1 日

x = 0:0.01:1;
figure(1)
filename = 'testnew51.gif';
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

#### 1 件のコメント

Coleman Barrie 2019 年 6 月 15 日
Thank you for writing this very helpful as a framework to modify

サインインしてコメントする。

Alec 2013 年 12 月 19 日
So it appears that `imwrite` now supports writing animated grayscale GIFs "When writing multiframe GIF images, X should be an 4-dimensional M-by-N-by-1-by-P array, where P is the number of frames to write."
But if I try to pass it an M-by-N-by-3-by-P it seems to treat each RGB color channel as a separate grayscale frame. Is there now way to write an animated color GIF without a for loop over the frames?

サインインしてコメントする。

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by