Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/substruct.m @ 20787:40ed9b46a800
new octave_value::string_value method with optional error message
* ov.h (octave_value::string_vector): New method.
ov-base.cc, ov-base.h (octave_base_value::string_vector):
New default method.
ov-str-mat.cc, ov-str-mat.h (octave_char_matrix_str::string_value):
New method.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 08 Oct 2015 16:43:22 -0400 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19404
diff
changeset
|
1 ## Copyright (C) 2006-2015 John W. Eaton |
10280 | 2 ## Copyright (C) 2010 VZLU Prague |
6167 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6167 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6167 | 19 |
20 ## -*- texinfo -*- | |
6744 | 21 ## @deftypefn {Function File} {} substruct (@var{type}, @var{subs}, @dots{}) |
19404 | 22 ## Create a subscript structure for use with @code{subsref} or @code{subsasgn}. |
23 ## | |
24 ## For example: | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
25 ## |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
26 ## @example |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
27 ## @group |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
28 ## idx = substruct ("()", @{3, ":"@}) |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
29 ## @result{} |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
30 ## idx = |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
31 ## @{ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
32 ## type = () |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
33 ## subs = |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
34 ## @{ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
35 ## [1,1] = 3 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
36 ## [1,2] = : |
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 ## @} |
19404 | 39 ## x = [1, 2, 3; |
40 ## 4, 5, 6; | |
41 ## 7, 8, 9]; | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
42 ## subsref (x, idx) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
43 ## @result{} 7 8 9 |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
44 ## @end group |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
45 ## @end example |
6167 | 46 ## @seealso{subsref, subsasgn} |
47 ## @end deftypefn | |
48 | |
49 ## Author: jwe | |
50 | |
51 function retval = substruct (varargin) | |
52 | |
19404 | 53 if (nargin < 2 || mod (nargin, 2) != 0) |
6167 | 54 print_usage (); |
55 endif | |
56 | |
19404 | 57 typ = varargin(1:2:nargin); |
58 sub = varargin(2:2:nargin); | |
59 braces = strcmp (typ, "()") | strcmp (typ, "{}"); | |
60 dots = strcmp (typ, "."); | |
61 if (all (braces | dots)) | |
62 cells = cellfun ("isclass", sub, "cell"); | |
63 chars = cellfun ("isclass", sub, "char"); | |
64 if (any (braces & !cells)) | |
65 error ("substruct: for TYPE == () or {}, SUBS must be a cell array"); | |
66 elseif (any (dots & !chars)) | |
67 error ("substruct: for TYPE == ., SUBS must be a character string"); | |
68 endif | |
69 else | |
70 error ('substruct: expecting TYPE to be one of "()", "{}", or "."'); | |
71 endif | |
72 | |
73 retval = struct ("type", typ, "subs", sub); | |
74 | |
6167 | 75 endfunction |
76 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
77 |
6167 | 78 %!test |
79 %! 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
|
80 %! 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
|
81 %! x(1,3).type = "."; |
6167 | 82 %! x(1,1).subs = {1,2,3}; |
10280 | 83 %! 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
|
84 %! x(1,3).subs = "foo"; |
10280 | 85 %! y = substruct ("()", {1,2,3}, "{}", {":"}, ".", "foo"); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
86 %! assert (x,y); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
87 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
88 %!error substruct () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
89 %!error substruct (1, 2, 3) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
90 %!error substruct ("x", 1) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
91 %!error substruct ("()", [1,2,3]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
92 %!error substruct (".", {1,2,3}) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
93 |