Mercurial > hg > octave-lyh
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 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2006, 2007 John W. Eaton |
6167 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6167 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6167 | 18 |
19 ## -*- texinfo -*- | |
6744 | 20 ## @deftypefn {Function File} {} substruct (@var{type}, @var{subs}, @dots{}) |
6167 | 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 | 43 ## @seealso{subsref, subsasgn} |
44 ## @end deftypefn | |
45 | |
46 ## Author: jwe | |
47 | |
48 function retval = substruct (varargin) | |
49 | |
50 nargs = nargin; | |
51 | |
52 if (nargs > 1 && mod (nargs, 2) == 0) | |
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 | 56 k = 1; |
57 for i = 1:2:nargs | |
58 t = varargin{i}; | |
59 dot = false; | |
60 switch (t) | |
61 case { "()", "{}" } | |
62 case "." | |
63 dot = true; | |
64 otherwise | |
65 error ("substruct: expecting type to be one of \"()\", \"{}\", or \".\""); | |
66 endswitch | |
67 s = varargin{i+1}; | |
68 if (dot) | |
69 if (! ischar (s)) | |
70 error ("substruct: for type == %s, subs must be a character string", t); | |
71 endif | |
72 elseif (! (iscell (s) || (ischar (s) && strcmp (s, ":")))) | |
73 error ("substruct: for type == %s, subs must be a cell array or \":\"", | |
74 t); | |
75 endif | |
76 typ{k} = t; | |
77 sub{k} = s; | |
78 k++; | |
79 endfor | |
80 retval = struct ("type", typ, "subs", sub); | |
81 else | |
82 print_usage (); | |
83 endif | |
84 | |
85 endfunction | |
86 | |
87 %!test | |
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 | 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 | 94 %! y = substruct ("()", {1,2,3}, "{}", ":", ".", "foo"); |
95 %! assert(x,y); | |
96 %!error assert(substruct); | |
97 %!error assert(substruct (1, 2, 3)); | |
98 %!error assert(substruct ("x", 1)); | |
99 %!error assert(substruct ("()", [1,2,3])); | |
100 %!error assert(substruct (".", {1,2,3})); |