function x=sched(f,dt,nout)
% sched - execute action after delay
%
% sched ::
%    (unit -> action (A1,...,AN)) ~'action with N return values',
%    nonneg ~'delay in seconds',
%    M:natural ~'number of output arguments to collect'
% -> unit -> action (A1,...,AM).
%
% After use, timer is marked for deletion but you must call
% timer_gc to actually delete it.

	if nargin<3, nout=0; end
	rc=cell(1,nout);
	tim=timer('TimerFcn',@(t,e)timfn,'StopFcn',@stopfn,'StartDelay',dt);
	start(tim);
	x=@()collect();

	function timfn, [rc{:}]=f(); end
	function stopfn(t,e), timer_release(t); end
	function varargout=collect(), 
		if isvalid(tim), wait(tim); end
		varargout=rc;
	end
end
	
