annotate scripts/miscellaneous/substruct.m @ 6167:d9e9c436abb0

[project @ 2006-11-15 15:22:46 by jwe]
author jwe
date Wed, 15 Nov 2006 15:22:46 +0000
parents
children b6c6587c1fb0
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 -*-
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {} setfield (@var{type}, @var{subs}, @dots{})
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 ##
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
25 ## @example
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
26 ## @group
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
27 ## @end group
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
28 ## @end example
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
29 ## @seealso{subsref, subsasgn}
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
30 ## @end deftypefn
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 ## Author: jwe
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
33
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
34 function retval = substruct (varargin)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
35
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
36 nargs = nargin;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
37
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
38 if (nargs > 1 && mod (nargs, 2) == 0)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
39 narg_pairs = nargs / 2;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
40 typ = cell (narg_pairs, 1);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
41 sub = cell (narg_pairs, 1);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
42 k = 1;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
43 for i = 1:2:nargs
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
44 t = varargin{i};
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
45 dot = false;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
46 switch (t)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
47 case { "()", "{}" }
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
48 case "."
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
49 dot = true;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
50 otherwise
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
51 error ("substruct: expecting type to be one of \"()\", \"{}\", or \".\"");
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
52 endswitch
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
53 s = varargin{i+1};
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
54 if (dot)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
55 if (! ischar (s))
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
56 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
57 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
58 elseif (! (iscell (s) || (ischar (s) && strcmp (s, ":"))))
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
59 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
60 t);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
61 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
62 typ{k} = t;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
63 sub{k} = s;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
64 k++;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
65 endfor
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
66 retval = struct ("type", typ, "subs", sub);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
67 else
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
68 print_usage ();
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
69 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
70
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
71 endfunction
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
72
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
73 %!test
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
74 %! x(1,1).type = "()";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
75 %! x(2,1).type = "{}";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
76 %! x(3,1).type = ".";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
77 %! x(1,1).subs = {1,2,3};
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
78 %! x(2,1).subs = ":";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
79 %! x(3,1).subs = "foo";
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
80 %! y = substruct ("()", {1,2,3}, "{}", ":", ".", "foo");
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
81 %! assert(x,y);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
82 %!error assert(substruct);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
83 %!error assert(substruct (1, 2, 3));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
84 %!error assert(substruct ("x", 1));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
85 %!error assert(substruct ("()", [1,2,3]));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
86 %!error assert(substruct (".", {1,2,3}));