This set of instructions detail how to create movies in MATLAB 5.1, convert them to the standard movie format MPEG and how to play them. Limited knowledge of MATLAB or the MPEG format is assumed. This code has not been verified for any other MATLAB version.
2) Resize the figure window to size of movie required.
3) Record the size of the plot window:
>> winsize = get(fig1,'Position');
4) Adjust size of this window to include the whole figure window (if
you require the axes, title and axis labels
in the movie):
>> winsize(1:2) = [0 0];
5) Set the number of frames:
6) Create the MATLAB movie matrix:
7) Fix the features of the plot window (ensures each frame of the movie is the same size):
8) Within a loop, plot each picture and save to MATLAB movie matrix:
>> for i=1:numframes
>> plot(X(i),Y(i)); % plot command
>> % add axis label, legends, titles, etc. in here
This procedure creates a movie stored in a special format that is only readable in MATLAB. The first thing you will want to do is to play the movie:
where fig1 is the figure handle, A is the movie matrix, 30 is the number of times to repeat movie, 3 is the number of frames per second and winsize is the size of the movie window. You can also save this movie to a file to be loaded another time or on another machine running MATLAB:
>> save filename.mat A
and to reload:
>> load filename.mat
Unfortunately, the format in which MATLAB stores the movie is very wasteful of precious memory. Each time you save a frame of the movie, MATLAB creates a pixel map of the plot window and stores it in a column of the movie matrix. This is very wasteful as lots of pixels are likely to stay the same for large portions of the movie. It would be better to save the first frame (and possibly a few later on) and just record the changes in each subsequent frame. This is the idea behind the MPEG movie format.
The basic outline of the MPEG format is that at the start of each movie, and when the picture changes a lot (like when the scene changes in a real movie), a snapshot (pixel map) of the movie is taken. Then only the changes between each subsequent frame are saved. If the movie does not change much from the original picture then a huge amount of space can be saved. This type of movie format can be created in MATLAB using a free program called MPGWRITE available from the MATLAB website, discussed under Requirements below.
where A is the movie matrix, jet (Red to Green to Blue) is the colour scheme to use (jet is usually the best, type help graph3d for others) and the string `movie.mpg' is the MPEG movie filename.
You now have an MPEG version of the MATLAB movie in the file movie.mpg. This format has the benefit that it does not need MATLAB to be to played, it can be run on any machine with an MPEG movie player. Lots of MPEG players are available, see Requirements below for details. A very good UNIX compatible MPEG player is called mpeg_play. Once installed, to play the movie just type in a command window:
% mpeg_play movie.mpg
or in MATLAB:
>> unix('mpeg_play movie.mpg')
This player has the same sort of buttons as a video recorder: play, stop, and pause, and a few extra ones: slow forward (one frame at a time) and loop (to repeat the movie from the beginning each time it finishes).
To play the MPEG movie you will need an MPEG player, freely available for most computer systems from http://www.mpeg.org/MPEG.
A good simple player for the UNIX platform is mpeg_play. MTV includes a lot of advanced features, but has licence restrictions. There does not seem to be any free UNIX compatible MPEG players that will allow you to resize the MPEG movie once it has been created, or set the number of frames per second.
There are many good MPEG players available for the Windows operating system, e.g. RealPlayer and the standard Windows Media Player.
>> A=moviein(numframes); % create the movie matrix
>> axis equal % fix the axes
>> for i=1:numframes
>> movie(A,10,3) % Play the MATLAB movie
>> save movie.mat A % save the MATLAB movie to a file
>> mpgwrite(A,jet,'movie.mpg'); % Convert the movie to MPEG format
>> % Notice the MPEG file is about a quarter of the size of the MATLAB movie file
>> unix('mpeg_play movie.mpg') % Play the MPEG movie
Hints and Tips
mathtools.net contains loads of useful links and information for technical computer programmers
Frame numbers inside the plotting window are often useful
Do not let any window overlap on the figure window while creating the movie, as this may get saved into the movie (a bug me thinks!)
It is not necessary to fix the plotting window features as suggested by 5 above, if the size of the axes are not changed when the graph is re-plotted
Save the original MATLAB movie into a file, and then compress it using something like gzip/winzip. It will reduce to a similar size as the MPEG movie file. This will allow you to do things like change the colour scheme of the MPEG movie later.
Remember the way Windows handles graphics is very memory intensive, so a movie that will run on a UNIX machine with a certain amount of memory may not run under Windows. As a rule of thumb on a 500 MhZ Pentium III, 256Mb RAM Windows PC it is worth keeping the number of frames down to about 250, whereas I have created 1000 frame movies on Sun Solaris machine with the same amount of physical memory.
If you have any comments to make on this guide then please email me at email@example.com.
The draft LaTeX code used to produce this document originally is available from
here and a draft postscript version here.
This site was last updated 15 June 2004.