function avislice(Xs,Ys,Zs,Cs,dim, thresh, clim, fps, name);
%AVISLICE.m--Creates a movie slicing along a dimension
%
%avislice(Xs,Ys,Zs,Cs,dim,thresh, clim, fps, name);
%
%Cs should be a volume of data on the grid defined by
%Xs, Ys, and Zs.  This function will use clearslice to
%create a slice along dimension dim:
%
%   dim=1 : slice along x
%   dim=2 : slice along y
%   dim=3 : slice along z
%
%The slices will be stored in frames of an avi movie with
%name indicted by name.  fps is the frames per second
%
%The parameter thresh is the threshold for clearslice: any
%values less than this will be transparent.  Clim should
%contain the color limits.

sz(1,:)=size(Xs);
sz(2,:)=size(Ys);
sz(3,:)=size(Zs);
sz(4,:)=size(Cs);

szcheck=sz-ones(4,1)*sz(1,:);
I=find(szcheck);
if(~isempty(I))
   error(['Xs,Ys,Zs, and Cs should be the same size']);
end


mov = avifile(name);
mov.Quality=100;
mov.Compression='None';
mov.Fps=fps;

str1='h=clearslice(Xs,Ys,Zs,Cs,';
switch dim
   case 1
      str2='Xs(j,1,1),[],[],thresh );';
      n=sz(1,1);
   case 2
      str2='[],Ys(1,j,1),[],thresh );';
      n=sz(1,2);
   case 3
      str2='[],[],Zs(1,1,j),thresh );';
      n=sz(1,3);
   otherwise
      error(['dim must be an integer between 1 and 3']);
end

j=1;
      

clf;
eval([str1,str2]);
set(gca,'clim',clim,'color','k','xcolor','y','ycolor','y','zcolor','y');
axlims=[span(Xs),span(Ys),span(Zs)];
axis(axlims);


F = getframe(gca);%capture the frame
mov = addframe(mov,F);%add it to the movie
for j=2:n
   disp(['Iteration ',int2str(j),': evaluating ',str1,str2]);
   eval([str1,str2]);
   set(gca,'clim',clim,'color','k','xcolor','y','ycolor','y','zcolor','y');
   axis(axlims);
   
	F = getframe(gca);
	mov = addframe(mov,F);

end
mov=close(mov);


%
function sp=span(X);
%SPAN.m--computes the span [min, max] of data
%
% sp=span(X);
%
%Returns the global min and max of X.  
%

sp(1)=min(X(:));
sp(2)=max(X(:));
