| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
movie(M)
movie(M,n)
movie(M,n,fps)
movie(h,...)
movie(h,M,n,fps,loc)
The movie function plays the movie defined by a matrix whose columns are movie frames (usually produced by getframe).
movie(M) plays the movie in matrix M once, using the current axes as the default target. If you want to play the movie in the figure instead of the axes, specify the figure handle (or gcf) as the first argument: movie(figure_handle,...). M must be an array of movie frames (usually from getframe).
movie(M,n) plays the movie n times. If n is negative, each cycle is shown forward then backward. If n is a vector, the first element is the number of times to play the movie, and the remaining elements make up a list of frames to play in the movie.
For example, if M has four frames then n = [10 4 4 2 1] plays the movie ten times, and the movie consists of frame 4 followed by frame 4 again, followed by frame 2 and finally frame 1.
movie(M,n,fps) plays the movie at fps frames per second. The default is 12 frames per second. Computers that cannot achieve the specified speed play as fast as possible.
movie(h,...) plays the movie centered in the figure or axes identified by the handle h.
movie(h,M,n,fps,loc) specifies loc, a four-element location vector, [x y 0 0], where the lower left corner of the movie frame is anchored (only the first two elements in the vector are used). The location is relative to the lower left corner of the figure or axes specified by handle h and in units of pixels, regardless of the object's Units property.
The movie function uses a default figure size of 560-by-420 and does not resize figures to fit movies with larger or smaller frames. To accommodate other frame sizes, you can resize the figure to fit the movie, as shown in the second example below.
movie only accepts 8-bit image frames; it does not accept 16-bit grayscale or 24–bit truecolor image frames.
Buffering the movie places all frames in memory. As a result, on Microsoft Windows and perhaps other platforms, a long movie (on the order of several hundred frames) can exhaust memory, depending on system resources. In such cases an error message is issued that says
??? Error using ==> movie Could not create movie frame
You can abort a movie by typing Ctrl-C.
Example 1: Animate the peaks function as you scale the values of Z:
Z = peaks; surf(Z); axis tight set(gca,'nextplot','replacechildren'); % Record the movie for j = 1:20 surf(sin(2*pi*j/20)*Z,Z) F(j) = getframe; end % Play the movie ten times movie(F,10)
Example 2: Specify figure when calling movie to fit the movie to the figure:
r = subplot(2,1,1)
Z = peaks; surf(Z);
axis tight
set(gca,'nextplot','replacechildren');
s = subplot(2,1,2)
Z = peaks; surf(Z);
axis tight
set(gca,'nextplot','replacechildren');
% Record the movie
for j = 1:20
axes(r)
surf(sin(2*pi*j/20)*Z,Z)
axes(s)
surf(sin(2*pi*(j+5)/20)*Z,Z)
F(j) = getframe(gcf);
pause(.0333)
end
% Play the movie; note that it does not fit the figure properly:
h2 = figure;
movie(F,10)
% Use the figure handle to make the frames fit:
movie(h2,F,10)
Example 3: With larger frames, first adjust the figure's size to fit the movie:
figure('position',[100 100 850 600])
Z = peaks; surf(Z);
axis tight
set(gca,'nextplot','replacechildren');
% Record the movie
for j = 1:20
surf(sin(2*pi*j/20)*Z,Z)
F(j) = getframe;
end
[h, w, p] = size(F(1).cdata); % use 1st frame to get dimensions
hf = figure;
% resize figure based on frame's w x h, and place at (150, 150)
set(hf, 'position', [150 150 w h]);
axis off
% tell movie command to place frames at bottom left
movie(hf,F,4,30,[0 0 0 0]);
aviread, getframe, frame2im, im2frame
Animation for related functions
See Example – Visualizing an FFT as a Movie for another example
![]() | movegui | movie2avi | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |