]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/refreshdata.m
update packages
[CreaPhase.git] / octave_packages / m / plot / refreshdata.m
1 ## Copyright (C) 2008-2012 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{workspace})
23 ## Evaluate any @samp{datasource} properties of the current figure and update
24 ## the plot if the corresponding data has changed.  If called with one or more
25 ## arguments @var{h} is a scalar or array of figure handles to refresh.  The
26 ## optional second argument @var{workspace} can take the following values.
27 ##
28 ## @table @asis
29 ## @item "base"
30 ## Evaluate the datasource properties in the base workspace.  (default).
31 ##
32 ## @item "caller"
33 ## Evaluate the datasource properties in the workspace of the function
34 ## that called @code{refreshdata}.
35 ## @end table
36 ##
37 ## An example of the use of @code{refreshdata} is:
38 ##
39 ## @example
40 ## @group
41 ## x = 0:0.1:10;
42 ## y = sin (x);
43 ## plot (x, y, "ydatasource", "y");
44 ## for i = 1 : 100
45 ##   pause (0.1);
46 ##   y = sin (x + 0.1*i);
47 ##   refreshdata ();
48 ## endfor
49 ## @end group
50 ## @end example
51 ## @end deftypefn
52
53 function refreshdata (h, workspace)
54
55   if (nargin == 0)
56     h = gcf ();
57     workspace = "base";
58   else
59     if (iscell (h))
60       h = [h{:}];
61     endif
62     if (!all (ishandle (h)) || !all (strcmp (get (h, "type"), "figure")))
63       error ("refreshdata: expecting a list of figure handles");
64     endif
65     if (nargin < 2)
66       workspace = "base";
67     else
68       if (   !ischar (workspace)
69           || !(strcmpi (workspace, "base")
70           || strcmpi (workspace, "caller")))
71         error ("refreshdata: expecting WORKSPACE to be \"base\" or ""caller\"");
72       else
73         workspace = tolower (workspace);
74       endif
75     endif
76   endif
77
78   h = findall (h);
79   objs = [];
80   props = {};
81
82   for i = 1 : numel (h)
83     obj = get (h (i));
84     fldnames = fieldnames (obj);
85     m = regexpi (fieldnames(obj), '^.+datasource$', "match");
86     idx = ! cellfun ("isempty", m);
87     if (any (idx))
88       tmp = m(idx);
89       props = [props; {vertcat(tmp{:})}];
90       objs  = [objs ; h(i)];
91     endif
92   endfor
93
94   for i = 1 : length (objs)
95     for j = 1 : length (props {i})
96       expr = get (objs(i), props{i}{j});
97       if (!isempty (expr))
98         val = evalin (workspace, expr);
99         prop =  props{i}{j}(1:end-6);
100         if (! isequal (get (objs(i), prop), val))
101           set (objs(i), props{i}{j}(1:end-6), val);
102         endif
103       endif
104     endfor
105   endfor
106 endfunction
107
108 %!demo
109 %! clf
110 %! x = 0:0.1:10;
111 %! y = sin (x);
112 %! plot (x, y, "ydatasource", "y");
113 %! for i = 1 : 100
114 %!   pause(0.1)
115 %!   y = sin (x + 0.1 * i);
116 %!   refreshdata(gcf(), "caller");
117 %! endfor