function make_movie_avi ( frame_max )

%*****************************************************************************80
%
%% make_movie_avi() creates an AVI animation from a sequence of frame files.
%
%  Discussion:
%
%    Before running this program close all figures and don't interfere
%    with the generated figures until the recording process is done.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 December 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer frame_max: the index of the final frame.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'make_movie_avi()\n' );
  fprintf ( 1, '  Create an AVI animation from a sequence of frame files.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Here we assume a sequence of frame files was created by\n' )
  fprintf ( 1, '  the MAKE_FRAMES program already.\n' );
%
%  Make sure we have the number of frames.
%
  if ( nargin < 1 )
    fprintf ( 1, '\n' );
    frame_max = input ( 'Enter the index of the final frame:  ' );
  end
%
%  Control how fast final movie will play; total time
%  of movie in seconds will be about (numframes)/(num_frames_per_second)
%
  FrameRate = 10;
  seconds = ( frame_max + 1 ) / FrameRate;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  This movie will contain frames 0 through %d.\n', frame_max );
  fprintf ( 1, '  The number of frames per second will be %f\n', FrameRate );
  fprintf ( 1, '  so the movie should take %f seconds to play.\n', seconds );
%
%  The VIDEOWRITER function creates a new Audio Video Interleaved (AVI) file.
%  We specify its name and FPS.
%
  filename = 'peak_movie.avi';
  V = VideoWriter ( filename );
  V.FrameRate = FrameRate;
  open ( V );
  fprintf ( 1, '  Creating animation file "%s"\n', filename );
%
%  Retrieve each frame.
%
  for frame_id = 0 : frame_max

    filename = sprintf ( 'frame%04d.jpg', frame_id );

    H = imshow ( filename );
    set ( H, 'EraseMode', 'xor' );
    set ( gca, 'nextplot', 'replacechildren' );
    shading ( 'interp' );
    axis ( 'tight' );
    axis ( 'square' );
    axis ( 'off' );
 
    frame = getframe ( gca );
    writeVideo ( V, frame );
%   aviobj = addframe ( V, frame );

  end
%
%  Tell MATLAB we have completed the movie.
%
  close ( V );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'make_movie_avi:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end

