# GIF animation in Matlab 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.

`x=rand(100,10);`

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.

```plot(x(:,k))
figure,
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,
plot(x(:,k);
f=getframe;
im(:,:,1,k) = rgb2ind(f.cdata,map,'nodither');
end

```

rgb2ind gets the pixel data to the im data structure.

3. Write out the image file.

```imwrite(im,map,'imagefile.gif','DelayTime',0,'LoopCount',inf);

```

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

## 10 thoughts on “GIF animation in Matlab in 3 steps”

1. Pingback: Top 5 posts | SukhbinderSingh.com

2. Wonderful blog! I found it while browsing on Yahoo News.

Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get
there! Cheers

Like

3. rou says:

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..

Like

• Have not faced the issue. I have checked this in windows system and it works ok. Please check.

Like

4. 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

Like

• 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.

Thanks

Like

5. percival says:

any hints on how to include the figure title in the image ?

Like