New in MATLAB R2022a: export graphics to animated GIFs

Adam Danz 2022년 3월 22일 (2022년 3월 22일에 수정됨)
최근 활동 Adam Danz 답글: 2025년 4월 14일 19:04

Starting in MATLAB R2022a, use the append option in exportgraphics to create GIF files from animated axes, figures, or other visualizations.

This basic template contains just two steps:

% 1. Create the initial image file
gifFile = 'myAnimation.gif';
exportgraphics(obj, gifFile);
% 2. Within a loop, append the gif image
for i = 1:20
      %   %   %   %   %   %    % 
      % Update the figure/axes %
      %   %   %   %   %   %    % 
      exportgraphics(obj, gifFile, Append=true);
  end

Note, exportgraphics will not capture UI components such as buttons and knobs and requires constant axis limits.

To create animations of images or more elaborate graphics, learn how to use imwrite to create animated GIFs .

Share your MATLAB animated GIFs in the comments below!

See Also

This Community Highlight is attached as a live script

Royi Avital
Royi Avital 2025년 4월 9일 18:32
It would be great to have Animtaed PNG (APNG) support. Much better quality in many cases.
Martin Baumann
Martin Baumann 2025년 4월 7일 22:37 (2025년 4월 10일 12:26에 수정됨)
How to set the background of the animated gif to transparent?
I trie (which does not work):
x = -10:0.5:10;
y = x.^2;
p = plot(x,y,"-o","MarkerFaceColor","red");
set(gcf, 'color', 'none');
set(gca, 'color', 'none');
for i=1:41
p.MarkerIndices = i;
exportgraphics(gcf,"parabola0.gif",'ContentType','vector',...
'BackgroundColor','none',"Append",true)
end
Adam Danz
Adam Danz 2025년 4월 14일 19:04
Transparent background is not supported for gif images. The BackgroundColor="none" name-value pair is supported when the ContentType is vector. But when exporting to gif, the ContentType is image and that Name-Value pair is ignored if specified.
Alekhya Hati
Alekhya Hati 2022년 6월 30일
How should I set the frame rate of a gif generated through exportgraphics?
Adam Danz
Adam Danz 2022년 6월 30일
exportgraphics does not currently have an option to control frame rate of GIFs.
The documentation for exportgraphics > Append argument suggests to use imwrite for more elaborate graphics. Also see Write Animated GIF.
Image Analyst
Image Analyst 2022년 3월 22일

Can a gif file be put into an Answers posting, and show up as animated?

Adam Danz
Adam Danz 2022년 3월 22일 (2022년 3월 23일에 수정됨)

A produced gif file can be uploaded and displayed as animated in the Answers forum but I don't think the Run feature can produce the animated image and display it on it's own, though I haven't tested it recently.