annotate scripts/miscellaneous/substruct.m @ 7016:93c65f2a5668

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