1 ## Copyright (C) 2006-2012 John W. Eaton
2 ## Copyright (C) 2010 VZLU Prague
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} substruct (@var{type}, @var{subs}, @dots{})
22 ## Create a subscript structure for use with @code{subsref} or
23 ## @code{subsasgn}. For example:
27 ## idx = substruct ("()", @{3, ":"@})
38 ## x = [1, 2, 3; 4, 5, 6; 7, 8, 9];
43 ## @seealso{subsref, subsasgn}
48 function retval = substruct (varargin)
52 if (nargs > 1 && mod (nargs, 2) == 0)
53 typ = varargin(1:2:nargs);
54 sub = varargin(2:2:nargs);
55 braces = strcmp (typ, "()") | strcmp (typ, "{}");
56 dots = strcmp (typ, ".");
57 if (all (braces | dots))
58 cells = cellfun ("isclass", sub, "cell");
59 chars = cellfun ("isclass", sub, "char");
60 if (any (braces &! cells))
61 error ("substruct: for TYPE == () or {}, SUBS must be a cell array");
62 elseif (any (dots &! chars))
63 error ("substruct: for TYPE == ., SUBS must be a character string");
66 error ("substruct: expecting TYPE to be one of \"()\", \"{}\", or \".\"");
69 retval = struct ("type", typ, "subs", sub);
77 %! x(1,1).type = "()";
78 %! x(1,2).type = "{}";
80 %! x(1,1).subs = {1,2,3};
81 %! x(1,2).subs = {":"};
82 %! x(1,3).subs = "foo";
83 %! y = substruct ("()", {1,2,3}, "{}", {":"}, ".", "foo");
85 %!error assert(substruct);
86 %!error assert(substruct (1, 2, 3));
87 %!error assert(substruct ("x", 1));
88 %!error assert(substruct ("()", [1,2,3]));
89 %!error assert(substruct (".", {1,2,3}));