1 ## Copyright (C) 2010-2012 Ben Abbott
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with Octave; see the file COPYING. If not, see
15 ## <http://www.gnu.org/licenses/>.
18 ## @deftypefn {Function File} {} setappdata (@var{h}, @var{name}, @var{value})
19 ## Set the named application data to @var{value} for the object(s) with
20 ## handle(s) @var{h}. If the application data with the specified name does
21 ## not exist, it is created.
24 ## Author: Ben Abbott <bpabbott@mac.com>
25 ## Created: 2010-07-15
27 function setappdata (h, varargin)
29 if (! (all (ishandle (h)) && mod (numel (varargin), 2) == 0))
30 error ("setappdata: invalid input");
34 if (! isfield (get (h(nh)), "__appdata__"))
35 addproperty ("__appdata__", h(nh), "any", struct ());
37 appdata = get (h(nh), "__appdata__");
38 for narg = 1:2:numel(varargin)
39 if (iscellstr (varargin{narg}))
40 ## Handle cell arrays like set() does.
41 set (h(nh), "__appdata__", appdata);
42 setappdata (h(nh), vertcat (varargin{narg}', varargin{narg+1}'){:});
43 appdata = get (h(nh), "__appdata__");
44 elseif (ischar (varargin{narg}))
45 appdata.(varargin{narg}) = varargin{narg+1};
47 error ("setappdata: invalid input");
50 set (h(nh), "__appdata__", appdata);
56 %! setappdata (0, "hello", "world")
57 %! assert (isappdata (0, "hello"), true)
58 %!assert (getappdata (0, "hello"), "world")