]> 6.8 Making movies in MATLAB This site will look best in a browser that supports web standards, but it is accessible to any browser or Internet device. We recommend the Mozilla Firefox browser. Software engineering and scientific computing

# 6.8 Making movies in MATLAB

If you are going to make a presentation then you may want to make a movie that you can include within it. The following example will show how to use the commands

 getframe movie movie2avi 

to make a movie in MATLAB. Before going through the walkthrough look at the help files for the above commands.

### Walkthrough

Here we are going to make a movie of a wave propagating through a membrane.

First, we clear all the variables and close all the open figures. We then make a 2D surface that represents the membrane.

 clear; close all; x=linspace(0,1,100); [X,Y] = meshgrid(x,x); 

The command ‘getframe’ will save the content of the currently selected figure as a frame, which we use to create a movie in the following loop

 N=100; % Number of frames for i = 1:N % Example of plot Z = sin(2*pi*(X-i/N)).*sin(2*pi*(Y-i/N)); surf(X,Y,Z) % Store the frame M(i)=getframe(gcf); % leaving gcf out crops the frame in the movie. end 

The variable ‘M’ is the movie. To view the movie in MATLAB you can use the command

movie(M)

Various options can be passed to ‘movie’. For example, to change the frame rate, look at the help files for more information. You will notice that some of it may appear cropped when you play the movie back, because of the size of the figure window. Don’t panic. It is not really cropped and you will see this when you save the movie to file. To get round this problem, you can first open a new figure and maximise it before running the ‘movie’ command.

To save the movie to file you can use the command

movie2avi(M,'WaveMovie.avi');

which saves the movie as the file ‘WaveMovie.avi’ in the current working directory. The movie produced is below.

Click on the play button to animate

Again there are many options you can pass to ‘movie2avi’ to change the frame rate or the type of compression used. Look at the help files for more information.

Once it is all brought together, the above code yields the following file that you can modify to create your own movies.

 clear; close all; % Creates a 2D Mesh to plot surface x=linspace(0,1,100); [X,Y] = meshgrid(x,x); N=100; % Number of frames for i = 1:N % Example of plot Z = sin(2*pi*(X-i/N)).*sin(2*pi*(Y-i/N)); surf(X,Y,Z) % Store the frame M(i)=getframe(gcf); % leaving gcf out crops the frame in the movie. end % Output the movie as an avi file movie2avi(M,'WaveMovie.avi');