annotate scripts/miscellaneous/substruct.m @ 14138:72c96de7a403 stable

maint: update copyright notices for 2012
author John W. Eaton <jwe@octave.org>
date Mon, 02 Jan 2012 14:25:41 -0500
parents c792872f8942
children 4d917a6a858b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
1 ## Copyright (C) 2006-2012 John W. Eaton
10280
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
2 ## Copyright (C) 2010 VZLU Prague
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
3 ##
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
5 ##
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
6 ## 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
7 ## 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
8 ## 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
9 ## your option) any later version.
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
10 ##
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
11 ## 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
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
15 ##
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
16 ## 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
17 ## 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
18 ## <http://www.gnu.org/licenses/>.
6167
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
10711
fbd7843974fa Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
23 ## @code{subsasgn}. For example:
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
24 ##
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
25 ## @example
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
26 ## @group
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
27 ## idx = substruct("()", @{3, ":"@})
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
28 ## @result{}
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
29 ## idx =
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
30 ## @{
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
31 ## type = ()
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
32 ## subs =
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
33 ## @{
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
34 ## [1,1] = 3
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
35 ## [1,2] = :
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 ## @}
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
38 ## 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
39 ## subsref(x, idx)
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
40 ## @result{} ans =
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
41 ## 7 8 9
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
42 ## @end group
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9774
diff changeset
43 ## @end example
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
44 ## @seealso{subsref, subsasgn}
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
45 ## @end deftypefn
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
46
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
47 ## Author: jwe
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
48
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
49 function retval = substruct (varargin)
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
50
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
51 nargs = nargin;
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
52
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
53 if (nargs > 1 && mod (nargs, 2) == 0)
10280
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
54 typ = varargin(1:2:nargs);
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
55 sub = varargin(2:2:nargs);
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
56 braces = strcmp (typ, "()") | strcmp (typ, "{}");
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
57 dots = strcmp (typ, ".");
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
58 if (all (braces | dots))
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
59 cells = cellfun ("isclass", sub, "cell");
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
60 chars = cellfun ("isclass", sub, "char");
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
61 if (any (braces &! cells))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
62 error ("substruct: for TYPE == () or {}, SUBS must be a cell array");
10280
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
63 elseif (any (dots &! chars))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
64 error ("substruct: for TYPE == ., SUBS must be a character string");
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
65 endif
10280
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
66 else
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
67 error ("substruct: expecting TYPE to be one of \"()\", \"{}\", or \".\"");
10280
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
68 endif
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
69
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
70 retval = struct ("type", typ, "subs", sub);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
71 else
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
72 print_usage ();
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
73 endif
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
74
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
75 endfunction
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
76
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
77 %!test
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
78 %! 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
79 %! 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
80 %! x(1,3).type = ".";
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
81 %! x(1,1).subs = {1,2,3};
10280
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
82 %! x(1,2).subs = {":"};
9774
fbf15a0f30f0 Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents: 7017
diff changeset
83 %! x(1,3).subs = "foo";
10280
b122dd3075ce vectorize substruct
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
84 %! y = substruct ("()", {1,2,3}, "{}", {":"}, ".", "foo");
6167
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
85 %! assert(x,y);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
86 %!error assert(substruct);
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
87 %!error assert(substruct (1, 2, 3));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
88 %!error assert(substruct ("x", 1));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
89 %!error assert(substruct ("()", [1,2,3]));
d9e9c436abb0 [project @ 2006-11-15 15:22:46 by jwe]
jwe
parents:
diff changeset
90 %!error assert(substruct (".", {1,2,3}));