function varargout=animdata(upd,X,varargin)
% animdata - use arbitrary function to animate data sequence
%
% animdata :: 
%    A->unit   ~'function to update animation',
%    seq A     ~'data sequence to plot',
%    options {
%       pre   :: action / @nop ~'called before each plot';
%       post  :: action / @nop ~'called after eacn plot';
%       defer :: bool   / 0    ~'if 1, just return state transformer function';
%       fps   :: nonneg / 0    ~'if positive, used timed iterator at this rate'
%    }
% -> (A->A,handle) ~'action to iterate',
%    A             ~'initial state'.



	opts=prefs('fps',0,'pre',@nop,'post',@nop,'defer',0,varargin{:});

	if opts.defer, 
		stfn=@plotnext_h; 
	elseif opts.fps>0
		stfn=@plotnext; 
		API=iterate_timed(stfn,X,1/opts.fps,opts); 
		if nargout==1, varargout{1}=API; end
	else
		stfn=@plotnext; 
		iterate(stfn,X,opts); 
		if nargout==1, varargout{1}={stfn,X}; else varargout={stfn,X}; end
	end


	function S=plotnext(S), upd(head(S)); S=next(S); end
	function [S,h]=plotnext_h(S),
		upd(head(S)); S=next(S);
		if nargout>1, h=get(gca,'title'); end
	end
end


