%% Record Frames and Play Movie Once
% Use the getframe function in a loop to record frames of the peaks example function, then play the movie frames once.
% Create a figure object h. Initialize the surface plot of the peaks function Z. Customize the figure axes.

h = figure;
Z = peaks;
surf(Z)
axis tight manual
ax = gca;
ax.NextPlot = 'replaceChildren';

% Preallocate a 40-element array M to store the movie frames.

loops = 40;
M(loops) = struct('cdata',[],'colormap',[]);

% For each iteration of j, capture each plot of function X as an individual frame. Store the frame in M.
% Set the 'Visible' property of the figure object to 'off' to hide the surface plots while calculating X.

h.Visible = 'off';
for j = 1:loops
    X = sin(j*pi/10)*Z;
    surf(X,Z)
    drawnow
    M(j) = getframe;
end

% Set the 'Visible' property of the figure to 'on' and play the movie in M once.

h.Visible = 'on';

% movie(M);
%
% Copyright 2015 The MathWorks, Inc.
%