6167
|
1 ## Copyright (C) 2006 John W. Eaton |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
6744
|
21 ## @deftypefn {Function File} {} substruct (@var{type}, @var{subs}, @dots{}) |
6167
|
22 ## Create a subscript structure for use with @code{subsref} or |
|
23 ## @code{subsasgn}. |
|
24 ## @seealso{subsref, subsasgn} |
|
25 ## @end deftypefn |
|
26 |
|
27 ## Author: jwe |
|
28 |
|
29 function retval = substruct (varargin) |
|
30 |
|
31 nargs = nargin; |
|
32 |
|
33 if (nargs > 1 && mod (nargs, 2) == 0) |
|
34 narg_pairs = nargs / 2; |
|
35 typ = cell (narg_pairs, 1); |
|
36 sub = cell (narg_pairs, 1); |
|
37 k = 1; |
|
38 for i = 1:2:nargs |
|
39 t = varargin{i}; |
|
40 dot = false; |
|
41 switch (t) |
|
42 case { "()", "{}" } |
|
43 case "." |
|
44 dot = true; |
|
45 otherwise |
|
46 error ("substruct: expecting type to be one of \"()\", \"{}\", or \".\""); |
|
47 endswitch |
|
48 s = varargin{i+1}; |
|
49 if (dot) |
|
50 if (! ischar (s)) |
|
51 error ("substruct: for type == %s, subs must be a character string", t); |
|
52 endif |
|
53 elseif (! (iscell (s) || (ischar (s) && strcmp (s, ":")))) |
|
54 error ("substruct: for type == %s, subs must be a cell array or \":\"", |
|
55 t); |
|
56 endif |
|
57 typ{k} = t; |
|
58 sub{k} = s; |
|
59 k++; |
|
60 endfor |
|
61 retval = struct ("type", typ, "subs", sub); |
|
62 else |
|
63 print_usage (); |
|
64 endif |
|
65 |
|
66 endfunction |
|
67 |
|
68 %!test |
|
69 %! x(1,1).type = "()"; |
|
70 %! x(2,1).type = "{}"; |
|
71 %! x(3,1).type = "."; |
|
72 %! x(1,1).subs = {1,2,3}; |
|
73 %! x(2,1).subs = ":"; |
|
74 %! x(3,1).subs = "foo"; |
|
75 %! y = substruct ("()", {1,2,3}, "{}", ":", ".", "foo"); |
|
76 %! assert(x,y); |
|
77 %!error assert(substruct); |
|
78 %!error assert(substruct (1, 2, 3)); |
|
79 %!error assert(substruct ("x", 1)); |
|
80 %!error assert(substruct ("()", [1,2,3])); |
|
81 %!error assert(substruct (".", {1,2,3})); |