File Exchange

image thumbnail

MATLAB Plot Gallery - Animation

version (1.78 MB) by MathWorks Plot Gallery Team
Create an animated GIF


Updated 19 Dec 2018

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This is an example of how to create animations and animated GIFs in MATLAB®. In this example, we will animate the motion of a double-pendulum. We solve the ordinary differential equation using "ode45".
Read about the "getframe" and "imwrite" functions in the MATLAB documentation.
For more examples, go to MATLAB Plot Gallery -

Cite As

MathWorks Plot Gallery Team (2021). MATLAB Plot Gallery - Animation (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (6)

Arpad Forberger

Same error message in 2021a update 1: Unable to perform assignment because the size of the left side is 420-by-560 and the size of the right side is 840-by-1120. Making the change according to Jose results that at the last step of the for loop (starting at line 47) the two color dots jump right at the end of the curves, no continuous motion.

David Garrison

The example works as written in R2020a with no changes required.

@Jose: Is it possible that you need to clear your workspace before running or that you have a function that shadows rgb2ind? The frames that are being generated are 420 x 560.


Unable to perform assignment because the size of the left side is 840-by-1120 and the size of the right side is 945-by-1260.

Error in Animation (line 61)
[mov(:,:,1,id), map] = rgb2ind(f.cdata, 256, 'nodither');

This is error I get in matlab2020a
even with Jose replacment
any ideas?

In Matlab 2020a it doesn't work.
Solved by changing line 59 with:
% Preallocate data (for storing frame data)
mov = zeros(height*2, width*2, 1, length(T), 'uint8');

Seyhan Özen

Neha Jain

I liked the animation in the form of a gif. I intend to use this in my web site :). Thank you

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!