comparison scripts/plot/refreshdata.m @ 8070:3b53b25e2550

Add data sources and line series
author David Bateman <dbateman@free.fr>
date Thu, 28 Aug 2008 12:23:54 -0400
parents
children 79c874fe5100
comparison
equal deleted inserted replaced
8069:c64c9581e9bf 8070:3b53b25e2550
1 ## Copyright (C) 2008 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} refreshdata ()
21 ## @deftypefnx {Function File} {} refreshdata (@var{h})
22 ## @deftypefnx {Function File} {} refreshdata (@var{h}, @var{ws})
23 ## Evaluates any datasource properties of the current figure and updates
24 ## the corresponding data. If call with one or more arguments @var{h} is
25 ## a scalar or array of figure handles which to refresh. The data
26 ## sources are by default evaluated in the "base" workspace but can also
27 ## be set in the "caller" workspace.
28 ##
29 ## An example of the use of refreshdata is
30 ##
31 ## @example
32 ## @group
33 ## x = 0:0.1:10;
34 ## y = sin (x);
35 ## plot (x, y, "ydatasource", "y");
36 ## for i = 1 : 100
37 ## pause(0.1)
38 ## y = sin (x + 0.1 * i);
39 ## refreshdata();
40 ## endfor
41 ## @end group
42 ## @end example
43 ##
44 ## @seealso{linkdata}
45 ## @end deftypefn
46
47 function refreshdata (h, ws)
48
49 if (nargin == 0)
50 h = gcf ();
51 ws = "base";
52 else
53 if (iscell (h))
54 h = [h{:}];
55 endif
56 if (!all (ishandle (h)) || !all (strcmp (get (h, "type"), "figure")))
57 error ("refreshdata: expecting a list of figure handles");
58 endif
59 if (nargin < 2)
60 ws = "base";
61 else
62 if (!ischar (ws) || !(strcmpi (ws, "base") || strcmpi (ws, "caller")))
63 error ("refreshdata: expecting workspace to be \"base\" or ""caller\"");
64 else
65 ws = tolower (ws);
66 endif
67 endif
68 endif
69
70 h = findall (h);
71 objs = [];
72 props = {};
73
74 for i = 1 : numel (h)
75 obj = get (h (i));
76 fldnames = fieldnames (obj);
77 m = regexpi (fieldnames(obj), "^.datasource$", "match");
78 idx = cellfun (@(x) !isempty(x), m);
79 if (any (idx))
80 props = [props; {cell2mat(m(idx))}];
81 objs = [objs ; h(i)];
82 endif
83 endfor
84
85 for i = 1 : length (objs)
86 for j = 1 : length (props {i})
87 expr = get (objs(i), props{i}{j});
88 if (!isempty (expr))
89 val = evalin (ws, expr);
90 prop = props{i}{j}(1:end-6);
91 if (! isequal (get (objs(i), prop), val))
92 set (objs(i), props{i}{j}(1:end-6), val);
93 endif
94 endif
95 endfor
96 endfor
97 endfunction
98
99 %!demo
100 %! x = 0:0.1:10;
101 %! y = sin (x);
102 %! plot (x, y, "ydatasource", "y");
103 %! for i = 1 : 100
104 %! pause(0.1)
105 %! y = sin (x + 0.1 * i);
106 %! refreshdata();
107 %! endfor