Saving Figures and Creating Gifs

MATLAB

writeGif


Create a .gif file animation. See Using_SaveFiles.mlx for a notebook of examples.

Syntax


writeGif(filename, iter)

writeGif(filename, iter, Name=Value)

Description


A

writeGif(filename, iter) saves a .gif file labeled filename. example

B

writeGif(filename, iter, Name=Value) saves a .gif file with additional options specified by one or more name-value pair arguments. For example, you can save to a supplemental folder. example

Examples


Simple example

Sine wave gif.

x = linspace(0, 2*pi, 100); % to evaluate

x_end = linspace(2*pi, 4*pi, 100); % make look bigger 

% sin 
% initialize
figure()
plot(x, sin(x), 'linewidth', 2);
ax = gca(); 

for i = 1:length(x_end)
    set(ax.Children(1), 'xdata', linspace(0, x_end(i)));
    set(ax.Children(1), 'ydata', sin(linspace(0, x_end(i))));
    writeGif('testGif1.gif', i)
end

FIG1

Slower animation

Cosine wave gif

x = linspace(0, 2*pi, 100); % to evaluate

x_end = linspace(2*pi, 4*pi, 100); % make look bigger 

% cos
% initialize
figure()
plot(x, sin(x), 'linewidth', 2);
ax = gca(); 

for i = 1:length(x_end)
    set(ax.Children(1), 'xdata', linspace(0, x_end(i)));
    set(ax.Children(1), 'ydata', sin(linspace(0, x_end(i))));
    writeGif('testGif2.gif', i, 'delayTime',0.5)
end

FIG2

Input Arguments


filename

Filename for .gif file.

Desired name of gif file. Final gif file = filename + .gif

Data Types: (char)

iter

Current iteration for gif

The iteration the loop is currently on.

Data Types: (double, scalar)

Name-Value Arguments

Specified optional pairs of Name=Value arguments. Name is the is the argument name and Value is the corresponding value. You can specify several name and value pair arguments in any order as Name1=Value1,...,NameN=ValueN.

Example: sup=false specifies a gif file to be saved in regular figure location (code_folder/figures/DD_MMM_YYYY).

delayTime

Time between frames for .gif file (default=0.1)

How long to delay between frames.

Data Types: (boolean)

More About


This function uses methodology best_save extensively.

Tips


I would suggest adding both writeGif.m and functionSignatures.json to a folder that is in your MATLAB path. The writeGif.m contains the function and the functionSignatures.json will you give custom suggestions and code completion for when you call writeGif in a script or notebook.

If you already have a functionSignatures.json file in your folder, just add the pertinent code to the original functionSignatures.json.

See also


best_save

Issues and Discussion


Issues and Discussion.

If you don’t know how to use github (or don’t want to), just send me an email.