GIF animation in Matlab in 3 steps

matlab gif in 3 steps Videos are cool. But if you need an animation to put in documentation or a PowerPoint then GIF is the way to go.

So building on the previous post of getting a video of your simulation from matlab, this post explains three simple steps to get a GIF animation from mat lab.

The steps are simple.

1. Define the data structure to hold the image data.
2. Plot and get the frame
3. Store the frame image in the data structure
4. Create the GIF animation.

As always lets begin with a data to plot.


Won’t explain much about the data structures and command as Matlab documentation does a good job at that.

I will explain the how to of doing it without much effort. Leave a comment if you are stuck, will try to resolve it.

1. Define the data structure or for shortcut let Matlab provide it.

f = getframe;
[im,map] = rgb2ind(f.cdata,256,'nodither');

2. Store and get the frame image in the data Structure.

im is the data structure.

for k=1:10,
im(:,:,1,k) = rgb2ind(f.cdata,map,'nodither');

rgb2ind gets the pixel data to the im data structure.

3. Write out the image file.


check Matlab help for other options.

Well that’s it. 3 steps for a gif animation from matlab


While I am with Matlab, I like this little bit of interesting history on the birth of Matlab. Shows how little bets bloom into something so big and useful.

Cleve Moler, the chairman of the computer-science department at the University of New Mexico, started developing MATLAB in the late 1970s. He designed it to give his students access to LINPACK and EISPACK without them having to learn Fortran. It soon spread to other universities and found a strong audience within the applied mathematics community. Jack Little, an engineer, was exposed to it during a visit Moler made to Stanford University in 1983. Recognizing its commercial potential, he joined with Moler and Steve Bangert. They rewrote MATLAB in C and founded MathWorks in 1984 to continue its development. These rewritten libraries were known as JACKPAC. In 2000, MATLAB was rewritten to use a newer set of libraries for matrix manipulation, LAPACK.

From wikipedia

9 thoughts on “GIF animation in Matlab in 3 steps

  1. Pingback: The Atoms of the Aerospace Industry |

  2. Pingback: Top 5 posts |

  3. Pingback: GIF animation in Python in 3 Steps |

  4. put when the gif is played out later, its taking a lot of time to play the entire gif…when matlab plots it, time taken is 20sec but when the gif is played out, it takes 60sec to do the complete run..any solutions..

  5. Pingback: A year of posts |

  6. I am trying to create an animated gif using the information from two vectors. I follow the steps shown above but I don’t get the expected result. Any help with this matter will be very useful. Thanks

    • Thanks Mariela. The steps work for me and just checked they work in matlab 10. Can you please show your code and the matlab version to see what errors you are getting.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s