annotate scripts/miscellaneous/substruct.m @ 6744:b6c6587c1fb0

[project @ 2007-06-18 19:56:28 by jwe]
author jwe
date Mon, 18 Jun 2007 19:56:28 +0000
parents d9e9c436abb0
children 93c65f2a5668
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
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
8 ## any later version.
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
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
18 ## 02110-1301, USA.
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
19
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
6744
b6c6587c1fb0 [project @ 2007-06-18 19:56:28 by jwe]
jwe
parents: 6167
diff changeset
21 ## @deftypefn {Function File} {} substruct (@var{type}, @var{subs}, @dots{})
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
22 ## Create a subscript structure for use with @code{subsref} or
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
23 ## @code{subsasgn}.
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
24 ## @seealso{subsref, subsasgn}
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
25 ## @end deftypefn
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
26
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
27 ## Author: jwe
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
28
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
29 function retval = substruct (varargin)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
30
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
31 nargs = nargin;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
32
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
33 if (nargs > 1 && mod (nargs, 2) == 0)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
34 narg_pairs = nargs / 2;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
35 typ = cell (narg_pairs, 1);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
36 sub = cell (narg_pairs, 1);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
37 k = 1;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
38 for i = 1:2:nargs
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
39 t = varargin{i};
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
40 dot = false;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
41 switch (t)
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 case "."
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
44 dot = true;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
45 otherwise
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
46 error ("substruct: expecting type to be one of \"()\", \"{}\", or \".\"");
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
47 endswitch
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
48 s = varargin{i+1};
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
49 if (dot)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
50 if (! ischar (s))
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
51 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
52 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
53 elseif (! (iscell (s) || (ischar (s) && strcmp (s, ":"))))
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
54 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
55 t);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
56 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
57 typ{k} = t;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
58 sub{k} = s;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
59 k++;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
60 endfor
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
61 retval = struct ("type", typ, "subs", sub);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
62 else
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
63 print_usage ();
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
64 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
65
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
66 endfunction
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
67
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
68 %!test
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
69 %! x(1,1).type = "()";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
70 %! x(2,1).type = "{}";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
71 %! x(3,1).type = ".";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
72 %! x(1,1).subs = {1,2,3};
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
73 %! x(2,1).subs = ":";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
74 %! x(3,1).subs = "foo";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
75 %! y = substruct ("()", {1,2,3}, "{}", ":", ".", "foo");
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
76 %! assert(x,y);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
77 %!error assert(substruct);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
78 %!error assert(substruct (1, 2, 3));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
79 %!error assert(substruct ("x", 1));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
80 %!error assert(substruct ("()", [1,2,3]));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
81 %!error assert(substruct (".", {1,2,3}));