Mercurial > hg > ishara
comparison general/algo/iterate_timed2.m @ 4:e44f49929e56
Adding reorganised general toolbox, now in several subdirectories.
author | samer |
---|---|
date | Sat, 12 Jan 2013 19:21:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:3f77126f7b5f | 4:e44f49929e56 |
---|---|
1 function Sched=iterate_timed(nextfn,X0,T,varargin) | |
2 % iterate_timed - Iterate function under control of timer | |
3 % | |
4 % iterate_timed :: | |
5 % (A=>A) ~'state transformer action', | |
6 % A ~'initial state', | |
7 % real ~'timer between updates in seconds' | |
8 % options { | |
9 % drawnow :: {0,1} /0 ~'call drawnow after each iteration'; | |
10 % busy_mode :: {'queue','drop'} /'queue' ~'See TIMER'; | |
11 % exec_mode :: {'fixedRate','fixedDelay','fixedSpacing'} /'fixedRate' ~'See TIMER'; | |
12 % its :: natural / inf ~'iteration limit'; | |
13 % onstart :: (A=>void)/ @nop ~'do this when timer starts'; | |
14 % onstop :: (A=>void)/ @nop ~'do this when timer stops'; | |
15 % onfinish :: (A=>void)/ @nop ~'do this when iterator terminates'; | |
16 % pre :: (A=>void)/ @nop ~'do before each iteration'; | |
17 % post :: (A=>void)/ @nop ~'do after each iteration'; | |
18 % defer :: bool / 0 ~'if true then don't start the timer'; | |
19 % -> timer, (A=>void) ~'function to seek to given state'. | |
20 % | |
21 % NB: Unlike ITERATE, this does NOT respect the id, save, and recover properties. | |
22 % Neither does it respect the OPTPAUSE property 'pause'. | |
23 | |
24 opts=prefs('its',inf,'drawnow',0, 'quiet', 0, 'defer', 0, ... | |
25 'busy_mode','queue','exec_mode','fixedRate', ... | |
26 'onfinish',@nop,'onstart',@nop,'onstop',@nop,varargin{:}); | |
27 | |
28 if ~isfield(opts,'pre') && ~isfield(opts,'post') && ~opts.drawnow | |
29 stfn=nextfn; % streamlined version with no checks | |
30 else % version with calls to pre,post, and drawnow | |
31 prefn=getparam(opts,'pre',@nop); | |
32 postfn=getparam(opts,'post',@nop); | |
33 stfn=@bracket; | |
34 end | |
35 | |
36 Sched=rsched(@schedfn,X0,T,0,opts,'defer',1, ... | |
37 'onstart',@onstart,'onstop',@onstop,'onfinish',@onfinish); | |
38 | |
39 if ~opts.defer, Sched.start(); end | |
40 | |
41 function [s,t_sched]=schedfn(s,dt,t_sched,t_actual), s=stfn(s); end | |
42 | |
43 function X=bracket(X), | |
44 prefn(X); X=nextfn(X); postfn(X); | |
45 if opts.drawnow, drawnow; end | |
46 end | |
47 | |
48 function onfinish(X,t_actual), opts.onfinish(X); end | |
49 | |
50 function onstart(X,t_sched,t_actual) | |
51 if ~opts.quiet, status('starting',X,t_actual); end | |
52 opts.onstart(X); | |
53 end | |
54 | |
55 function onstop(X,t_actual) | |
56 if ~opts.quiet, status('stopping',X,t_actual); end | |
57 opts.onstop(X); | |
58 end | |
59 end | |
60 | |
61 function status(msg,x,t), | |
62 fprintf('| %s at %s with %s\n',msg,mat2str(t),tostring(x)); | |
63 end | |
64 | |
65 |