by

Making a Movie in MATLAB

Although it creates bulky AVI files, MATLAB can relatively easily make movies of animated figures. The code below prototypes how this is done.

% define the number of frames in your movie
numberOfFrames = 1200;
% use a loop to generate frames of the movie
for ii = 1:numberOfFrames
    % insert code to update data to be plotted
    % plot data
    figure(1); clf;
    plot(x,y);
    % insert code to format plot any way you want
    drawnow; % force the plot to be displayed
    M(ii) = getframe(1); % save figure 1 as an element of
    % a movie object called M
end

% use the movie2avi function to save the movie
% there are several parameters you can specify
% the only one usually worth messing with is 
% frames per second (fps)
% 20 fps will make this 1200 frame movie 60 s long
movie2avi(M, 'filename', 'fps', 20);

I hope this helps. The AVI will probably be over a GB in size. Use handbrake or ffmpeg to convert the movie made by MATLAB to a more compressed format like MP4.

2 Comments


  1. // Reply

    I’m new to matlab. Im trying to make a movie using this function

    F(x)=x^2 * e^(-x/4)

    could you help me with his


    1. // Reply

      Mike – Do you mean you want to make a movie of this function being plotted? Try something like this:
      % before movie for loop define the range of x values to be plotted and an empty y vector
      x = 0:0.1:100;

      % in the movie for loop update the y vector with the next element to be plotted where ii is the index of the iteration through the loop currently being run
      temp = x(ii)^2 * exp(-x(ii)/4);
      y = [y temp];

      % then plot x vs. y and repeat.

Leave a Reply

Your email address will not be published.