]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/substruct.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / substruct.m
1 ## Copyright (C) 2006-2012 John W. Eaton
2 ## Copyright (C) 2010 VZLU Prague
3 ##
4 ## This file is part of Octave.
5 ##
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.
10 ##
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.
15 ##
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/>.
19
20 ## -*- texinfo -*-
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:
24 ##
25 ## @example
26 ## @group
27 ## idx = substruct ("()", @{3, ":"@})
28 ##      @result{}
29 ##        idx =
30 ##        @{
31 ##          type = ()
32 ##          subs =
33 ##          @{
34 ##            [1,1] =  3
35 ##            [1,2] = :
36 ##          @}
37 ##        @}
38 ## x = [1, 2, 3; 4, 5, 6; 7, 8, 9];
39 ## subsref (x, idx)
40 ##    @result{} 7  8  9
41 ## @end group
42 ## @end example
43 ## @seealso{subsref, subsasgn}
44 ## @end deftypefn
45
46 ## Author:  jwe
47
48 function retval = substruct (varargin)
49
50   nargs = nargin;
51
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");
64       endif
65     else
66       error ("substruct: expecting TYPE to be one of \"()\", \"{}\", or \".\"");
67     endif
68
69     retval = struct ("type", typ, "subs", sub);
70   else
71     print_usage ();
72   endif
73
74 endfunction
75
76 %!test
77 %! x(1,1).type = "()";
78 %! x(1,2).type = "{}";
79 %! x(1,3).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");
84 %! assert(x,y);
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}));