MATLAB Tips and Tricks

Wednesday, December 22, 2004

Creating movies in MATLAB

If you want to show how your plots change with time, a movie is a good idea. Especially since MATLAB can produce movies that can be embedded in your presentations, or be seen with any good ol' movie player. Here's how:

The idea is to create plot figures and compile them as frames of your movie.

% Create a new figure, and position it
    fig1 = figure;
    winsize = get(fig1,'Position');
    winsize(1:2) = [0 0];
% Create movie file with required parameters
    fps= 25;
    outfile = sprintf('%s',outFileName)
    mov = avifile(outfile,'fps',fps,'quality',100);
% Ensure each frame is of the same size
    set(fig1,'NextPlot','replacechildren');
    for i=1:numframes
        plot(x,y); % generate your plot
% put this plot in a movieframe
% In case plot title and axes area are needed
%     F = getframe(fig1,winsize);
% For clean plot without title and axes
        F = getframe;
        mov = addframe(mov,F);
    end
% save movie
    mov = close(mov);

Voila! (Make sure that you do not open any window over your plot window while MATLAB is compiling your movie, else those windows will become part of your movie)

2 Comments:

  • At 5:42 AM, Anonymous Oliver said…

    avi.m does not seem to exist.
    I'm getting the following error:


    ??? Failed to open file.

    Error in ==> avifile.avifile at 163
    aviobj.FileHandle = avi('open',filename);

     
  • At 6:26 AM, Anonymous Oliver said…

    Hi, me again.
    I have found that it gives this error intermittently. When the error comes back, clearing any avi objects seems to help.
    This is done by typing the followig in at the command window:

    clear mex

    I'm still not sure, however, why the error happens. Symptomatic of being a newb, I guess. ;)

     

Post a Comment

<< Home