samer@0: % uiterate - Run a processing unit for some number of iterations samer@0: % samer@0: % uiterate :: samer@0: % unit({}, _, _) ~'live processing unit', samer@0: % T:natural ~'number of iterations to run (can be inf)', samer@0: % options { samer@0: % draw :: boolean/false ~'whether or not to call drawnow after each iteration'; samer@0: % quiet :: boolean/false ~'whether or not to suppress progress messages'; samer@0: % chunk :: natural/1 ~'print progress every chunk interations' samer@0: % } samer@0: % -> action (). samer@0: % samer@0: % This function accepts the live processing unit associated samer@0: % with an arrow (as created by with_arrow). The arrow must samer@0: % have zero inputs. Outputs are ignored. If inf iterations are samer@0: % requested, the system is run until an EOF exception is caught. samer@0: samer@0: function uiterate(u,its,varargin) samer@0: uiter1(u,its,u.process,'label','uiterate',varargin{:}); samer@0: end samer@0: samer@0: function itsdone=uiter1(u,its,itfn,varargin) samer@37: opts=options('draw',1,'quiet',0,'chunk',1,varargin{:}); samer@0: quiet=opts.quiet; draw=opts.draw; samer@0: chunk=opts.chunk; samer@0: u.starting(); samer@0: try samer@0: i=1; samer@0: while i<=its, samer@0: if ~quiet && mod(i,chunk)==0, fprintf(' %s: %d \r',opts.label,i); end samer@0: itfn(); samer@0: if draw, drawnow; end samer@0: i=i+1; samer@0: end samer@0: catch ex samer@0: if ~iseof(ex) samer@0: if ~quiet, fprintf('exception at %d iterations.\n',i); end samer@0: u.stopping(); samer@0: rethrow(ex); samer@0: end samer@0: end samer@0: itsdone=i-1; samer@0: if ~quiet, fprintf('done %d iterations.\n',itsdone); end samer@0: u.stopping(); samer@0: end