function makeavi(stack,frames,name)

%movframes=figure('Visible','off');
%set(fig,'DoubleBuffer','on');

[ht wd]=size(stack(:,:,1));

smin=min(min(min(stack)));
smax=max(max(max(stack)));

movframes=figure('Units','pixels','Position',[100 100 wd ht]);

%set(gca,'xlim',[-80 80],'ylim',[-80 80],...
%       'NextPlot','replace','Visible','off')

mov = avifile(name)



for k=1:frames
imagesc(stack(:,:,k),[smin smax]); colormap(gray);

 %   set(h,'EraseMode','xor');
    F = getframe(gca);
    mov = addframe(mov,F);
end
mov = close(mov);