comparison scripts/miscellaneous/getappdata.m @ 19298:f0dcb44826f6

Overhaul appdata family of functions (getappdata, isappdata, rmappdata, setappdata). * getappdata.m: Redo docstring. Match function variable names to documentation. Place input validation first and make error messages more specific. Add BIST tests. * isappdata.m: Redo docstring. Match function variable names to documentation. Make input validation more precise. Use try/catch block for performance improvement. Add BIST tests. * rmappdata.m: Redo docstring. Improve input validation. Remove one for loop through vectorization. Add BIST tests. * setappdata.m: Redo docstring. Make input validation more precise. Use try/catch block for 80% performance improvement. Add BIST tests.
author Rik <rik@octave.org>
date Sat, 18 Oct 2014 18:10:52 -0700
parents 7bbe3658c5ef
children 0e1f5a750d00
comparison
equal deleted inserted replaced
19297:281a36ad4907 19298:f0dcb44826f6
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{value} =} getappdata (@var{h}, @var{name}) 20 ## @deftypefn {Function File} {@var{value} =} getappdata (@var{h}, @var{name})
21 ## @deftypefnx {Function File} {@var{appdata} =} getappdata (@var{h}) 21 ## @deftypefnx {Function File} {@var{appdata} =} getappdata (@var{h})
22 ## Return the @var{value} of the application data @var{name} for the graphics
23 ## object with handle @var{h}.
22 ## 24 ##
23 ## Return the @var{value} for named application data for the object(s) with 25 ## @var{h} may also be a vector of graphics handles. If no second argument
24 ## handle(s) @var{h}. 26 ## @var{name} is given then @code{getappdata} returns a structure,
25 ## 27 ## @var{appdata}, whose fields correspond to the appdata properties.
26 ## @code{getappdata(@var{h})} returns a structure, @var{appdata}, whose fields
27 ## correspond to the appdata properties.
28 ## 28 ##
29 ## @seealso{setappdata, guidata, get, set, getpref, setpref} 29 ## @seealso{setappdata, isappdata, rmappdata, guidata, get, set, getpref, setpref}
30 ## @end deftypefn 30 ## @end deftypefn
31 31
32 ## Author: Ben Abbott <bpabbott@mac.com> 32 ## Author: Ben Abbott <bpabbott@mac.com>
33 ## Created: 2010-07-15 33 ## Created: 2010-07-15
34 34
35 function val = getappdata (h, name) 35 function value = getappdata (h, name)
36 36
37 if (all (ishandle (h)) && nargin == 2 && ischar (name)) 37 if (nargin < 1 || nargin > 2)
38 print_usage ();
39 endif
40
41 if (! all (ishandle (h(:))))
42 error ("getappdata: H must be a scalar or vector of graphic handles");
43 endif
44
45 if (nargin == 2)
46 if (! ischar (name))
47 error ("getappdata: NAME must be a string");
48 endif
49
38 ## FIXME: Is there a better way to handle non-existent appdata 50 ## FIXME: Is there a better way to handle non-existent appdata
39 ## and missing fields? 51 ## and missing fields?
40 val = cell (numel (h), 1); 52 value = cell (numel (h), 1);
41 appdata = struct (); 53 appdata = struct ();
42 for nh = 1:numel (h) 54 for i = 1:numel (h)
43 try 55 try
44 appdata = get (h(nh), "__appdata__"); 56 appdata = get (h(i), "__appdata__");
45 end_try_catch 57 end_try_catch
46 if (! isfield (appdata, name)) 58 if (isfield (appdata, name))
47 appdata.(name) = []; 59 value(i) = {appdata.(name)};
60 else
61 value(i) = {[]};
48 endif 62 endif
49 val(nh) = {appdata.(name)};
50 endfor 63 endfor
51 if (nh == 1) 64
52 val = val{1}; 65 if (i == 1)
66 value = value{1};
53 endif 67 endif
54 elseif (ishandle (h) && numel (h) == 1 && nargin == 1) 68
69 else # nargin == 1
70 if (numel (h) != 1)
71 error ("getappdata: Only one handle H may be used when fetching appdata");
72 endif
55 try 73 try
56 val = get (h, "__appdata__"); 74 value = get (h, "__appdata__");
57 catch 75 catch
58 val = struct (); 76 value = struct ();
59 end_try_catch 77 end_try_catch
60 else
61 error ("getappdata: invalid input");
62 endif 78 endif
63 79
64 endfunction 80 endfunction
65 81
82
83 %!test
84 %! unwind_protect
85 %! setappdata (0, "%data1%", ones (3), "%data2%", "hello world");
86 %! assert (getappdata (0, "%data1%"), ones (3));
87 %! assert (getappdata (0, "%data2%"), "hello world");
88 %! appdata = getappdata (0);
89 %! name1 = "%data1%"; name2 = "%data2%";
90 %! assert (appdata.(name1), ones (3));
91 %! assert (appdata.(name2), "hello world");
92 %! unwind_protect_cleanup
93 %! rmappdata (0, "%data1%", "%data2%");
94 %! end_unwind_protect
95
96 ## Test input validation
97 %!error getappdata ()
98 %!error getappdata (1,2,3)
99 %!error <H must be a scalar .* graphic handle> getappdata (-1, "hello")
100 %!error <NAME must be a string> getappdata (0, 1)
101 %!error <Only one handle H may be used when fetching appdata> getappdata ([0 0])
102