function make_movie_mat ( frame_max )

%*****************************************************************************80
%
%% make_movie_mat() creates an MAT animation file from a sequence of frame files.
%
%  Discussion:
%
%    The result is a MATLAB .MAT file which can be played within MATLAB.
%
%    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_mat()\n' );
  fprintf ( 1, '  Create a MATLAB animation file (".MAT")\n' );
  fprintf ( 1, '  from frame files created by make_frames().\n' )
%
%  Make sure we have the number of frames.
%
  if ( nargin < 1 )
    fprintf ( 1, '\n' );
    frame_max = input ( 'Enter the index of the last frame:  ' );
  end
%
%  Control how fast final movie will play; total time
%  of movie in seconds will be about (numframes)/(num_frames_per_second)
%
  fps = 10;
  seconds = ( frame_max + 1 ) / fps;

  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', fps );
  fprintf ( 1, '  so the movie should take %f seconds to play.\n', seconds );
%
%  Close all figures.
%
  close ( 'all' );
%
%  Open a figure window, and get a pointer to it.
%
  figure_handle = figure ( 1 );
%
%  Get the first frame and display it.
%
  frame_id = 0;
  filename = sprintf ( 'frame%04d.jpg', frame_id );
%
%  Get the size of the window.
%
  window_size = get ( figure_handle, 'Position' );
  window_size
  window_size(1:2) = [ 0, 0 ];
  window_size
%
%  Create the MATLAB movie object.
%
  A = moviein ( frame_max + 1, figure_handle );
%
%  Force all frames to be the same size.
%
  set ( figure_handle, 'NextPlot', 'replacechildren' );
%
%  Retrieve each file.
%
  for frame_id = 0 : frame_max
    filename = sprintf ( 'frame%04d.jpg', frame_id );
    imshow ( filename );
    A(:,frame_id + 1) = getframe ( figure_handle );
  end
%
%  Save the movie in a MAT file.
%
  filename = 'peak_movie.mat';
  save ( filename, 'A' );
  fprintf ( 1, '  Movie saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'make_movie_mat:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end

