MATLAB: How to Create an Animation
14 May 2009 Daniel Sutoyo 9 comments 4,175 views
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.
9 Responses to “MATLAB: How to Create an Animation”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Mmmm…
I just want to ask, whether to make an animation with matlab do not spend a lot of resources ???…
Because, I intend to make an animation of a change point to a map, which continues to change.
is there need to plot every point ???
@ Derict
I don’t see your plot functions. You don’t need to replot per say. But it is the easiest.
If you do not want to replot, what you will need to do is alter the data stored in the existing figure object. It will be a children of axes which is children of figure. More lines of code, but less memory intensive.
Derict,
This code won’t work. Take a look at this line:
In the second, third, and fourth clauses of the IF statement, you’re using a single equals sign — that attempts to assign a value to teta_user, and assignment’s not allowed inside an IF statement like that.
Note that even if you replaced those single equals signs with double equals signs (to check for equality) your code still won’t work — in that case, the IF statement would only execute if teta_user is greater than or equal to 0 AND equal to 90 AND equal to 180 AND equal to 270 AND less than or equal to 360. There’s no number that’s simultaneously equal to 90 and equal to 180.
If you want to make sure that teta_user is one of 90, 180, or 270 then use || (OR) or ISMEMBER:
if (teta_user == 90) || (teta_user == 180) || (teta_user == 270)
% or
if ismember(teta_user, [90 180 270])
Finally, to prevent roundoff error from interfering, you probably want to ROUND teta_user before or inside the comparison, or check to make sure that the difference between teta_user and one of [90, 180, 270] is small. This technique is described in question 6.1 of the FAQ for the comp.soft-sys.matlab newsgroup:
http://matlabwiki.mathworks.com/MATLAB_FAQ
Just wondering, can I use images (JPEG?) to create animation using MATLAB and play it in a GUI? =D
@ elie
you can certainly import the JPEG and plot them again… but might be easier to use a 3rd party animator and make the AVI and then import the AVI into the MATLAB GUI.
Went to a few different sites but this one works the best (and easiest!) If you want to export to an AVI file from there, just use movie2avi(M,’filename.avi’)
hi, I am doing a small program to simulate a chase between a dog , a cat ,a mouse . Is it possilbe to simulate the chase with matlab?
how to animation charactor
my graduation project is a “Chess Playing Robot”, I want to draw an operate the robot on Matlab, I don’t know anything about animation in Matlab.
do you have any websites or links ??