function sMovie2Gif(Mov,filename) 
% sMovie2Gif - saves MATLAB movie matrix into a series of gif files
%      sMovie2Gif(Mov,filename)
%	   Mov - MATLAB movie matrix, obtained by 'getframe'
%	   filename - path and filename for the files
%      The filenames are produced by concatenating frame number 
%      to the filename, for instance, if filename='mymov':
%	   mymov01.gif mymov02.gif ... mymov23.gif
%
%  WARNING: function uses MOVIE function. Set the figure size and axis prior
%	    to starting this function
%
% see also: movie, getframe, moviein


if nargin ~= 2,
   disp('Wrong number of arguments');
   return
end;

n = size(Mov,2);
NoOfZeros = floor( log(n)/log(10) ) + 1;

clf; axis('off');
colormap('default');
map = colormap;
movie(Mov(:,1));
[A,map] = capture;
imshow(A,map); 
disp('Select the crop region');
[B,rect] = imcrop;

for i = 1:n,
   clf; axis('off'); colormap('default');
   movie(Mov(:,i)); 
   [A,map] = capture; imshow(A,map); 
   B = imcrop(A, rect);
   gifwrite(B, map, sprintf([filename '%0' num2str(NoOfZeros) 'd.gif'],i) );
end;

return
