Matlab Logo Animation is an effective way to display the changes in your data. MATLAB is already capable of creating a variety of 2D and 3D plots, but using animation introduces another dimension to your data visualization.

In this tutorial, you will learn how to create a series of images and collectively show them as an animation (see below for an example). The two MATLAB commands you will need to know are GETFRAME and MOVIE.


Contents

Quick Overview

So how does one create an animation video in MATLAB? Since an animation is essentially a series of images in quick succession, the key to creating an animation is to create a bunch of images at different intervals of time. In the following section, we are going to create a series of images that change over a period of time. Then, we will string these images together to create an animation.

Creating the Images

Each image is treated as a frame of the movie. An image could be any MATLAB plot that you already know how to create. The majority of the work in creating a movie involves the creation of all the images. This is usually done through a FOR loop. Let’s take the following as an example:

% Create a grid of data points
[x,y] = meshgrid(-3:0.05:3,-3:0.05:3);

% Loop to create a series of images
for i = 1:20
    % Sinusoid with its phase changing
    s = sin(x + i*0.05*pi);

    % Mathematical function of PEAKS

z =  3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
   - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
   - 1/3*exp(-(x+1).^2 - y.^2);

% Multpliy the changing sinusoid with the PEAKS
z = s.*z;

% Graph my modified PEAKS
surf(z)
zlim([-10 10])
axis off

light
shading interp
% axis off
set(gcf,'Color','white')

% Capture the figure as a frame and store it in M
M(i) = getframe;
end

Showing the Animation

To show the movie you just have to use the function MOVIE to play the frames stored in M. You can specify how many times the movie gets played and how many frames per second.

% movie(M,n,fps)
% M - stored frames
% n - loops, there are additional options with this parameter
% fps - frames per second

movie(M,1,32)

You should see something similar to the following:


Next Time

In the next post we will cover more examples of animations. Additionally I will show how you can export these animations to various formats. In the mean time, see if you can create your own animations. If you would like to share your animations and be featured in the next post, please contact us through our contact form.