Highlights
Follow


New in MATLAB R2022a: export graphics to animated GIFs

Adam Danz on 22 Mar 2022 (Edited on 22 Mar 2022)
Latest activity Reply by Adam Danz on 30 Jun 2022

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

Alekhya Hati
Alekhya Hati on 30 Jun 2022
How should I set the frame rate of a gif generated through exportgraphics?
Adam Danz
Adam Danz on 30 Jun 2022
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 on 22 Mar 2022

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

Adam Danz
Adam Danz on 22 Mar 2022 (Edited on 23 Mar 2022)

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.