%% How to make an animated GIF
% This example animates the vibration of a membrane, captures a series of
% screen shots, and saves the animation as a GIF image file.
%
% Copyright 2008-2010 The MathWorks, Inc. 
%%
% <<../DancingPeaks.gif>>
%%
% The resulted animated GIF was embedded in this HTML page using the Image
% cell markup (see 
% <http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f6-30186.html#breug1i help for markup formatting>). 
%%
% Here's the M code.

% makes axis no more than neccesssary
axis tight

% sets up so show a frame, then next time around, deletes previous frame/children
set(gca,'nextplot','replacechildren','visible','off')
f = getframe;
[im,map] = rgb2ind(f.cdata,256,'nodither');
im(1,1,1,20) = 0;
x = -1:.1:1;
for k = [1:20 20:1] 
  % create a visual
  plot(x,k*x);
  % stuff the visible plot into f
  f = getframe;

  % this holds all the frames, after converted into rgb format
  im(:,:,1,k) = rgb2ind(f.cdata,map,'nodither');
end

% writes all the frames into a animation called AnimationTest.gif
imwrite(im,map,'AnimationTest.gif','DelayTime',0,'LoopCount',inf) %g443800
%%
% For more details about GIF settings |DelayTime| and |LoopCount| for desired
% effect see the 
% <http://www.mathworks.com/access/helpdesk/help/techdoc/ref/imwrite.html#f25-752355 help for |imwrite/gif|>. 

