File: setappdata.m

package info (click to toggle)
octave 10.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 145,388 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 659; xml: 192
file content (139 lines) | stat: -rw-r--r-- 5,076 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
########################################################################
##
## Copyright (C) 2010-2025 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {} setappdata (@var{h}, @var{name}, @var{value})
## @deftypefnx {} {} setappdata (@var{h}, @var{name1}, @var{value1}, @var{name2}, @var{value2}, @dots{})
## @deftypefnx {} {} setappdata (@var{h}, @{@var{name1}, @var{name2}, @dots{}@}, @{@var{value1}, @var{value2}, @dots{}@})
## Set the application data @var{name} to @var{value} for the graphics object
## with handle @var{h}.
##
## @var{h} may also be a vector of graphics handles.  If the application data
## with the specified @var{name} does not exist, it is created.
##
## Multiple @var{name}/@var{value} pairs can be specified.  Alternatively, a
## cell array of @var{names} and a corresponding cell array of @var{values} can
## be specified.  For details on obtaining a list of valid application data
## properties, @pxref{XREFgetappdata,,@code{getappdata}}.
##
## @seealso{getappdata, isappdata, rmappdata, guidata, get, set, getpref,
## setpref}
## @end deftypefn

function setappdata (h, varargin)

  if (nargin < 3)
    print_usage ();
  endif

  h = h(:).';
  if (! all (ishghandle (h)))
    error ("setappdata: H must be a scalar or vector of graphic handles");
  elseif (mod (numel (varargin), 2) != 0)
    error ("setappdata: NAME/VALUE arguments must occur in pairs");
  endif

  if (iscellstr (varargin{1}))
    if (nargin != 3)
      error ("setappdata: only 3 arguments possible when NAME is a cellstr");
    elseif (! iscell (varargin{2}))
      varargin{2} = varargin(2);  # convert to cell
    endif
    names = varargin{1};
    values = varargin{2};
    n_names = numel (names);
    n_value = numel (values);
    if (n_value == 1 && n_names > 1)
      values = repmat (values, [1, n_names]);
    elseif (n_names != n_value)
      error ("setappdata: number of NAME and VALUE arguments must match");
    endif
    varargin = cell (1, 2*numel (names));
    varargin(1:2:end) = names;
    varargin(2:2:end) = values;

  elseif (! all (cellfun ("isclass", varargin(1:2:end), "char")))
    error ("setappdata: NAME must be a string or cellstr");
  endif

  for hg = h
    appdata = get (hg, "__appdata__");

    ## Slow, but not likely to be that many elements in loop
    for narg = 1:2:numel (varargin)
      appdata.(varargin{narg}) = varargin{narg+1};
    endfor

    set (hg, "__appdata__", appdata);
  endfor

endfunction


%!test
%! unwind_protect
%!   setappdata (0, "%hello%", "world");
%!   assert (isappdata (0, "%hello%"), true);
%!   assert (getappdata (0, "%hello%"), "world");
%! unwind_protect_cleanup
%!   rmappdata (0, "%hello%");
%! end_unwind_protect

%!test
%! unwind_protect
%!   setappdata (0, "%data1%", ones (3), "%data2%", "hello world");
%!   assert (getappdata (0, "%data1%"), ones (3));
%!   assert (getappdata (0, "%data2%"), "hello world");
%!   setappdata (0, "%data1%", zeros (3));
%!   assert (getappdata (0, "%data1%"), zeros (3));
%! unwind_protect_cleanup
%!   rmappdata (0, "%data1%", "%data2%");
%! end_unwind_protect

%!test
%! unwind_protect
%!   setappdata (0, {"%data1%", "%data2%"}, {ones(3), "hello world"});
%!   assert (getappdata (0, "%data1%"), ones (3));
%!   assert (getappdata (0, "%data2%"), "hello world");
%!   setappdata (0, "%data1%", zeros (3));
%!   assert (getappdata (0, "%data1%"), zeros (3));
%!   rmappdata (0, "%data1%", "%data2%");
%!   setappdata (0, {"%data1%", "%data2%"}, pi);
%!   assert (getappdata (0, "%data1%"), pi);
%!   assert (getappdata (0, "%data2%"), pi);
%! unwind_protect_cleanup
%!   rmappdata (0, "%data1%", "%data2%");
%! end_unwind_protect

## Test input validation
%!error <Invalid call> setappdata ()
%!error <Invalid call> setappdata (0)
%!error <Invalid call> setappdata (0, "name")
%!error <H must be a scalar .* graphic handle> setappdata (-1, "foo", "bar")
%!error <NAME/VALUE arguments must occur in pairs> setappdata (0, "1", 2, "3")
%!error <only 3 arguments possible> setappdata (0, {"1"}, 2, "3", 4)
%!error <number of NAME and VALUE arguments must match>
%! setappdata (0, {"1", "2"}, {2, 3, 4})
%!error <NAME must be a string> setappdata (0, 1, 2)