Generating .gifs from Matlab Figures

If your results have some form of time dependence, you would probably like to see them in motion. Movie file formats such as .avi, .mpg, etc. can be bulky, low-quality, and unreliable for presentations. Graphics Interchange Files (.gif) are a comparatively lightweight, cross-platform alternative. A .gif file is basically just a sequence of images stitched together, and Matlab can perform this stitching for you.

(Disclaimer: Matlab can also generate movies in .avi/.mpg format, but I tend to avoid that for the reasons mentioned above. Perhaps someone can comment on the “dynamic” capabilities of Matplotlib and R — these might be nice alternatives, but I’ve never used them).

For a simple example, consider a wave function sin(wt + kx). You can make a time-varying .gif of this function using the following code:

x = 0:0.01:2*pi;
omega = 0.5;
k = 1;

for t=1:50

    plot(x, sin(omega*t + k*x), 'linewidth', 2, 'color', 'red');
    ylim([-1 1]);
    xlim([0 2*pi]);
    grid on;

    % gif utilities
    set(gcf,'color','w'); % set figure background to white
    drawnow;
    frame = getframe(1);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);
    outfile = 'sinewave.gif';

    % On the first loop, create the file. In subsequent loops, append.
    if t==1
        imwrite(imind,cm,outfile,'gif','DelayTime',0,'loopcount',inf);
    else
        imwrite(imind,cm,outfile,'gif','DelayTime',0,'writemode','append');
    end

end

This will produce the following (if the image appears static, you may need to click it to open in a new tab):

sinewave

From this example, you can see how a movie of any figure can be created fairly easily. Just stick the plotting commands inside a loop, and at the end of each iteration, append to the .gif file.

If you’d like to see a more compelling example, I’ve been using this to generate .gifs of time-varying sensitivity analysis for a watershed. In the figure below, you can see how the spatially distributed cell sensitivity (top-left) changes with time. The top-right panel shows precipitation, and the bottom panel shows the overall time period with a red line to denote the current time.

movie-event-1

(Again, you may need to click the image to see the animation). After large rainfall events, sensitivity is concentrated at the watershed outlet (bottom). Over time, it propagates up toward the headwaters. This type of thing is very difficult to convey with static figures, but it’s much more intuitive with a .gif. I hope you find this useful, and thanks for reading.

10 thoughts on “Generating .gifs from Matlab Figures

  1. Pingback: Matlab and Matplotlib Plotting Examples | Water Programming: A Collaborative Research Blog

  2. The red bar is just a vertical line using the “plot” command. So if you do “hold on”, and then “plot([t 0], [t 350])” for example, it should make a vertical line at x=t. Or choose whichever timestep you want.

  3. Hi,
    Using your code the grid lines and box disappear in my pc. Do you know why this could be?
    Thanks for the code, it makes it supper easy and automatic to create gifs đŸ™‚

  4. Hi Ignacio,

    I’m not sure why this could be happening. This is an older post, but still seems to work (Matlab R2015a / OSX). It could be a PC problem, or maybe there is some global plot style setting that is overriding the “grid on” command.

    Thanks, sorry I can’t be more help.
    -Jon

  5. Pingback: Water Programming Blog Guide (Part I) – Water Programming: A Collaborative Research Blog

  6. Great solution! I was wondering how to suppress the “real time” generation of the frames themselves prior to the gif being fully formed. For me this code generates the frames “live” in a figure, which is slowing down the code. I imagine this is very simple to solve but I can’t seem to get it to “silently” generate the gif.

Leave a reply to Almie Cancel reply