annotate scripts/miscellaneous/substruct.m @ 10122:9d1a14e12431

Update docs and add tests for container functions
author Thorsten Meyer <thorsten.meyier@gmx.de>
date Sun, 17 Jan 2010 13:31:42 +0100
parents fbf15a0f30f0
children b122dd3075ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2006, 2007 John W. Eaton
6167
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
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
22 ## @code{subsasgn}. For example:
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
23 ##
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
24 ## @example
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
25 ## @group
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
26 ## idx = substruct("()", @{3, ":"@})
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
27 ## @result{}
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
28 ## idx =
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
29 ## @{
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
30 ## type = ()
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
31 ## subs =
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
32 ## @{
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
33 ## [1,1] = 3
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
34 ## [1,2] = :
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
35 ## @}
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
36 ## @}
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
37 ## x = [1, 2, 3; 4, 5, 6; 7, 8, 9];
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
38 ## subsref(x, idx)
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
39 ## @result{} ans =
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
40 ## 7 8 9
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
41 ## @end group
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
42 ## @end example
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
43 ## @seealso{subsref, subsasgn}
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
44 ## @end deftypefn
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
45
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
46 ## Author: jwe
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
47
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
48 function retval = substruct (varargin)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
49
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
50 nargs = nargin;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
51
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
52 if (nargs > 1 && mod (nargs, 2) == 0)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
53 narg_pairs = nargs / 2;
9774
fbf15a0f30f0 Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents: 7017
diff changeset
54 typ = cell (1, narg_pairs);
fbf15a0f30f0 Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents: 7017
diff changeset
55 sub = cell (1, narg_pairs);
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
56 k = 1;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
57 for i = 1:2:nargs
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
58 t = varargin{i};
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
59 dot = false;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
60 switch (t)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
61 case { "()", "{}" }
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
62 case "."
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
63 dot = true;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
64 otherwise
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
65 error ("substruct: expecting type to be one of \"()\", \"{}\", or \".\"");
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
66 endswitch
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
67 s = varargin{i+1};
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
68 if (dot)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
69 if (! ischar (s))
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
70 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
71 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
72 elseif (! (iscell (s) || (ischar (s) && strcmp (s, ":"))))
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
73 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
74 t);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
75 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
76 typ{k} = t;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
77 sub{k} = s;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
78 k++;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
79 endfor
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
80 retval = struct ("type", typ, "subs", sub);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
81 else
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
82 print_usage ();
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
83 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
84
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
85 endfunction
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
86
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
87 %!test
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
88 %! x(1,1).type = "()";
9774
fbf15a0f30f0 Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents: 7017
diff changeset
89 %! x(1,2).type = "{}";
fbf15a0f30f0 Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents: 7017
diff changeset
90 %! x(1,3).type = ".";
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
91 %! x(1,1).subs = {1,2,3};
9774
fbf15a0f30f0 Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents: 7017
diff changeset
92 %! x(1,2).subs = ":";
fbf15a0f30f0 Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents: 7017
diff changeset
93 %! x(1,3).subs = "foo";
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
94 %! y = substruct ("()", {1,2,3}, "{}", ":", ".", "foo");
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
95 %! assert(x,y);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
96 %!error assert(substruct);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
97 %!error assert(substruct (1, 2, 3));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
98 %!error assert(substruct ("x", 1));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
99 %!error assert(substruct ("()", [1,2,3]));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
100 %!error assert(substruct (".", {1,2,3}));