File Exchange

image thumbnail


version (506 KB) by Chad Greene
An easy gif generator


Updated 30 Jan 2021

View Version History

View License

Check out the Examples tab ^^^.

This function is intended to streamline the process of making gifs. Simply call


to first the first frame, and then call


to write each subsequent frame. That's it.

Cite As

Greene, Chad A., et al. “The Climate Data Toolbox for MATLAB.” Geochemistry, Geophysics, Geosystems, vol. 20, no. 7, American Geophysical Union (AGU), July 2019, pp. 3774–81, doi:10.1029/2019gc008392.

View more styles

Comments and Ratings (50)

Di Wu


Yudong Chen

Chad Greene

@Christopher: Thanks for the suggestion! I've included it in the latest update.

Les Daniel

I am trying record a gif within a for loop with multiple if/else if/else statements, and my code runs all the way to the end, but the gif only plays my initial frame for 2 minutes. I am opening and closing the same plot with different points shown, Does anyone have any idea why it would only show the initial frame for 2 minutes?

Christopher Wong

Just a suggestion - and this is really easy for anyone to do themselves by modifying your script. But it would be nice if there was some option that overwrites the existing git file by default: `gif('filename.gif','overwrite',true)`.

Christopher Wong


Sam Horne

Chad Greene

@Nicholas: The only way I know is to repeat the same frame. So to pause on frame 10 it would be something like the following. Repeating the same frame 48 times like I'm doing below would pause the animation for 48/fr seconds, if fr is the frame rate.

for k =1:Nframes
<plotting stuff>
if k==10
for kk = 1:48

Nicholas Weinberg

Is there any way to pause the gif for a certain number of seconds on the last frame before it loops? Sorry if there's an obvious answer, just can't seem to figure it out


Incredible easy to use (compared to 'imwrite'). Thx a lot for creating!

John Just

Alfredo Cano

Adam Danz

Stefan Kiss

Suggestion: add
```if any(strcmpi(varargin,'skipfirst')), return, end```
to line 140, to delay writing of the first frame.

Xiaoxuan Liang

very useful!

Elyasaf Cohen

Steven Hernandez

Javed Iqbal

Can we use the same function on multiple images to save them as a gif? and How??

Kieren Smith


Sanzhar Askaruly

Thanks for the function.
To save sequence of images, I have to use imshow(myImage) and gcf. Is it possible to save without visualizing, directly?

Taiwo Fasae


Xinmin Liu


Thanks so much!

Trinh Cong Dan

Jonas Klingebiel

Robert Sweeney

Thanks Chad

Chad Greene

@tanq: Just specify 'frame',gcf to capture the entire figure rather than the default frame (gca), which only captures the current axes. By capturing the entire figure it should include the title.


Hello Chad, thanks for great work, but how could add the title in loop? thank a lot

Chad Greene

Aidan: To include a colorbar specify 'frame',gcf when calling gif the first time. That will ensure everything within the figure gets giffed. (The default frame is gca, which does not include colorbars outside the current axis.)

Xel Ch

Thanks Ethan. For some reason, I am still getting error messages with the line gif('myfile.gif') . When I comment this part out, the loop works but only runs through one time then stops.

For reference, I have all the code in one file instead of "creating a new gif file" like the example suggests, because I couldn't figure out how to do that. Not sure if that matters as it seems to still work for one loop. Thanks again!

Ethan Douglas

Is there a minimum 'DelayTime'? I seem to be able to increase it beyond the default, but when I go any lower (0.005 for instance), the gif is still only doing 1/15 frames per second.

Ethan Douglas

Alexandar, make sure the gif.m function is in your current directory when calling it

Xel Ch

When I try to use the run the line gif('myfile.gif') , I am given the error message "Cannot find an exact (case-sensitive) match for 'gif'". Any idea how to get around this? Thanks!

Wolfgang Schwanghart

A well-documented and easy-to-use function that saved me a lot of time! Thanks, Chad.

Aidan Starr

Great function, however is there a way to include a colorbar in the gif? Cheers,

Fei Lu

Dan Sazer

Please disregard my previous question; earlier in the script I had written "hold on" which led to the undesired overlay.

Dan Sazer

Great function! I was wondering if there's a way to remove the alpha channel from each frame? When using this function to make a sequence of plots, all the lines just superimpose over each other.

Anselm Griffin

In response to Fabian . I got the same but then I read the documentation . The command is
web('myfile.gif') put in whatever your gif is called


Fabian Garcia

Thomas McKay-Smith

Fabian Garcia

While using the function it only records the first frame. When i use gif to add the subsequent frames nothing happens. I only get a gif with one static frame. Any ideas ?


Chad Greene

Hi Andrea, thanks for the note. The error you mention is strange--there is no "cq" function in the gif function, and I don't think it's in anything the gif function calls either. Is cq a typo in your script, by chance? If you can post the whole error message that might help us track down the cause.


In Matlab (R2017a) I get the following error when running the example:

Undefined function 'cq' for input arguments of type 'uint8'

Any suggestions?

Stefan Appelhoff

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!