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)

6 comments:

Anonymous 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);

Anonymous 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. ;)

alexvicegrab said...

Neat, clear walkthrough! Not too little and not too much, just perfect. Thank you :)

Unknown said...

HI,

I am getting this error when i try to use the provided code.

??? Undefined function or variable 'outFileName'.

Kindly advice.

Ranitox

Anonymous said...

@ranitox

you should do it like this:

outfile = sprintf('%s','outFileName')

Anonymous said...

Hey guys do you know how we can adjust the movie to play in a specific axes size?
I mean i created a figure and provided it with the position property parameters. I have a structure holding the frames of a movie. Now i need to show this movie in the figure that i have created. But when i run it, the movie plays but its at a larger size than the figure size. I mean the movie frames dont scale down to the figure size.
Any suggestions on what i could do?

Moving to a new website

This website was intended to be a little collection of tips and tricks, but I haven't been able to update it as often as I originally in...