Mercurial > octave-nkf
annotate doc/interpreter/container.txi @ 12175:2090995ca588
Correct en-dash,em-dash instances in docstrings.
Add a few more operators to operator index.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 26 Jan 2011 20:51:20 -0800 |
parents | a4b0e292107f |
children | 1eb430d9a1b9 |
rev | line source |
---|---|
11523 | 1 @c Copyright (C) 1996-2011 John W. Eaton |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
5 @c Octave is free software; you can redistribute it and/or modify it | |
6 @c under the terms of the GNU General Public License as published by the | |
7 @c Free Software Foundation; either version 3 of the License, or (at | |
8 @c your option) any later version. | |
9 @c | |
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT | |
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 @c for more details. | |
14 @c | |
15 @c You should have received a copy of the GNU General Public License | |
16 @c along with Octave; see the file COPYING. If not, see | |
17 @c <http://www.gnu.org/licenses/>. | |
3439 | 18 |
6837 | 19 @node Data Containers |
20 @chapter Data Containers | |
21 @cindex containers | |
22 | |
6939 | 23 Octave includes support for two different mechanisms to contain |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 arbitrary data types in the same variable. Structures, which are C-like, |
6837 | 25 and are indexed with named fields, and cell arrays, where each element |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
26 of the array can have a different data type and or shape. Multiple |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
27 input arguments and return values of functions are organized as |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
28 another data container, the comma separated list. |
6837 | 29 |
30 @menu | |
31 * Data Structures:: | |
32 * Cell Arrays:: | |
33 * Comma Separated Lists:: | |
34 @end menu | |
35 | |
36 @node Data Structures | |
37 @section Data Structures | |
38 @cindex structures | |
39 @cindex data structures | |
40 | |
41 Octave includes support for organizing data in structures. The current | |
42 implementation uses an associative array with indices limited to | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
43 strings, but the syntax is more like C-style structures. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
44 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
45 @menu |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
46 * Basic Usage and Examples:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
47 * Structure Arrays:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
48 * Creating Structures:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
49 * Manipulating Structures:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
50 * Processing Data in Structures:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
51 @end menu |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
52 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
53 @node Basic Usage and Examples |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
54 @subsection Basic Usage and Examples |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
55 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
56 Here are some examples of using data structures in Octave. |
6837 | 57 |
58 Elements of structures can be of any value type. For example, the three | |
59 expressions | |
60 | |
61 @example | |
62 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
63 x.a = 1; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
64 x.b = [1, 2; 3, 4]; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
65 x.c = "string"; |
6837 | 66 @end group |
67 @end example | |
68 | |
12175
2090995ca588
Correct en-dash,em-dash instances in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11537
diff
changeset
|
69 @opindex . |
6837 | 70 @noindent |
12175
2090995ca588
Correct en-dash,em-dash instances in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11537
diff
changeset
|
71 create a structure with three elements. The @samp{.} character separates |
2090995ca588
Correct en-dash,em-dash instances in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11537
diff
changeset
|
72 the structure name from the field name and indicates to Octave that this |
2090995ca588
Correct en-dash,em-dash instances in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11537
diff
changeset
|
73 variable is a structure. To print the value of the |
2090995ca588
Correct en-dash,em-dash instances in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11537
diff
changeset
|
74 structure you can type its name, just as for any other variable: |
6837 | 75 |
76 @example | |
77 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
78 x |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
79 @result{} x = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
80 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
81 a = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
82 b = |
6837 | 83 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
84 1 2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
85 3 4 |
6837 | 86 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
87 c = string |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
88 @} |
6837 | 89 @end group |
90 @end example | |
91 | |
92 @noindent | |
93 Note that Octave may print the elements in any order. | |
94 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
95 Structures may be copied just like any other variable: |
6837 | 96 |
97 @example | |
98 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
99 y = x |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
100 @result{} y = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
101 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
102 a = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
103 b = |
6837 | 104 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
105 1 2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
106 3 4 |
6837 | 107 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
108 c = string |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
109 @} |
6837 | 110 @end group |
111 @end example | |
112 | |
113 Since structures are themselves values, structure elements may reference | |
114 other structures. The following statements change the value of the | |
115 element @code{b} of the structure @code{x} to be a data structure | |
116 containing the single element @code{d}, which has a value of 3. | |
117 | |
118 @example | |
119 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
120 x.b.d = 3; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
121 x.b |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
122 @result{} ans = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
123 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
124 d = 3 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
125 @} |
6837 | 126 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
127 x |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
128 @result{} x = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
129 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
130 a = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
131 b = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
132 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
133 d = 3 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
134 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
135 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
136 c = string |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
137 @} |
6837 | 138 @end group |
139 @end example | |
140 | |
141 Note that when Octave prints the value of a structure that contains | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
142 other structures, only a few levels are displayed. For example: |
6837 | 143 |
144 @example | |
145 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
146 a.b.c.d.e = 1; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
147 a |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
148 @result{} a = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
149 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
150 b = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
151 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
152 c = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
153 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
154 1x1 struct array containing the fields: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
155 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
156 d: 1x1 struct |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
157 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
158 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
159 @} |
6837 | 160 @end group |
161 @end example | |
162 | |
163 @noindent | |
164 This prevents long and confusing output from large deeply nested | |
11474 | 165 structures. The number of levels to print for nested structures may be |
166 set with the function @code{struct_levels_to_print}, and the function | |
167 @code{print_struct_array_contents} may be used to enable printing of the | |
168 contents of structure arrays. | |
6837 | 169 |
170 @DOCSTRING(struct_levels_to_print) | |
171 | |
11474 | 172 @DOCSTRING(print_struct_array_contents) |
173 | |
6837 | 174 Functions can return structures. For example, the following function |
175 separates the real and complex parts of a matrix and stores them in two | |
176 elements of the same structure variable. | |
177 | |
178 @example | |
179 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
180 function y = f (x) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
181 y.re = real (x); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
182 y.im = imag (x); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
183 endfunction |
6837 | 184 @end group |
185 @end example | |
186 | |
187 When called with a complex-valued argument, @code{f} returns the data | |
188 structure containing the real and imaginary parts of the original | |
189 function argument. | |
190 | |
191 @example | |
192 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
193 f (rand (2) + rand (2) * I) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
194 @result{} ans = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
195 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
196 im = |
6837 | 197 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
198 0.26475 0.14828 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
199 0.18436 0.83669 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
200 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
201 re = |
6837 | 202 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
203 0.040239 0.242160 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
204 0.238081 0.402523 |
6837 | 205 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
206 @} |
6837 | 207 @end group |
208 @end example | |
209 | |
210 Function return lists can include structure elements, and they may be | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
211 indexed like any other variable. For example: |
6837 | 212 |
213 @example | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
214 [ x.u, x.s(2:3,2:3), x.v ] = svd ([1, 2; 3, 4]); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
215 x |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
216 @result{} x = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
217 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
218 u = |
6837 | 219 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
220 -0.40455 -0.91451 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
221 -0.91451 0.40455 |
6837 | 222 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
223 s = |
6837 | 224 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
225 0.00000 0.00000 0.00000 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
226 0.00000 5.46499 0.00000 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
227 0.00000 0.00000 0.36597 |
6837 | 228 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
229 v = |
6837 | 230 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
231 -0.57605 0.81742 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
232 -0.81742 -0.57605 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
233 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
234 @} |
6837 | 235 @end example |
236 | |
237 It is also possible to cycle through all the elements of a structure in | |
238 a loop, using a special form of the @code{for} statement | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
239 (@pxref{Looping Over Structure Elements}). |
6837 | 240 |
241 @node Structure Arrays | |
242 @subsection Structure Arrays | |
243 | |
244 A structure array is a particular instance of a structure, where each of | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
245 the fields of the structure is represented by a cell array. Each of |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
246 these cell arrays has the same dimensions. Conceptually, a structure |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
247 array can also be seen as an array of structures with identical |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
248 fields. An example of the creation of a structure array is |
6837 | 249 |
250 @example | |
251 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
252 x(1).a = "string1"; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
253 x(2).a = "string2"; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
254 x(1).b = 1; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
255 x(2).b = 2; |
6837 | 256 @end group |
257 @end example | |
258 | |
259 @noindent | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
260 which creates a 2-by-1 structure array with two fields. Another way |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
261 to create a structure array is with the @code{struct} function |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
262 (@pxref{Creating Structures}). As previously, to print the value of |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
263 the structure array, you can type its name: |
6837 | 264 |
265 @example | |
266 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
267 x |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
268 @result{} x = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
269 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
270 1x2 struct array containing the fields: |
6837 | 271 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
272 a |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
273 b |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
274 @} |
6837 | 275 @end group |
276 @end example | |
277 | |
278 Individual elements of the structure array can be returned by indexing | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
279 the variable like @code{@var{x}(1)}, which returns a structure with |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
280 two fields: |
6837 | 281 |
282 @example | |
283 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
284 x(1) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
285 @result{} ans = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
286 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
287 a = string1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
288 b = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
289 @} |
6837 | 290 @end group |
291 @end example | |
292 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
293 Furthermore, the structure array can return a comma separated list of |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
294 field values (@pxref{Comma Separated Lists}), if indexed by one of its |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
295 own field names. For example: |
6837 | 296 |
297 @example | |
298 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
299 x.a |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
300 @result{} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
301 ans = string1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
302 ans = string2 |
6837 | 303 @end group |
304 @end example | |
305 | |
8584
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
306 Here is another example, using this comma separated list on the |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
307 left-hand side of an assignment: |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
308 |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
309 @example |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
310 @group |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
311 [x.a] = deal("new string1", "new string2"); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
312 x(1).a |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
313 @result{} ans = new string1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
314 x(2).a |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
315 @result{} ans = new string2 |
8584
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
316 @end group |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
317 @end example |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
318 |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
319 Just as for numerical arrays, it is possible to use vectors as indices |
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
320 (@pxref{Index Expressions}): |
8584
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
321 |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
322 @example |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
323 @group |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
324 x(3:4) = x(1:2); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
325 [x([1,3]).a] = deal("other string1", "other string2"); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
326 x.a |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
327 @result{} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
328 ans = other string1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
329 ans = new string2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
330 ans = other string2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
331 ans = new string2 |
8584
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
332 @end group |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
333 @end example |
848f7d3e589c
Update the examples for structure arrays in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8541
diff
changeset
|
334 |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
335 The function @code{size} will return the size of the structure. For |
6837 | 336 the example above |
337 | |
338 @example | |
339 @group | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
340 size(x) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
341 @result{} ans = |
6837 | 342 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
343 1 4 |
6837 | 344 @end group |
345 @end example | |
346 | |
347 Elements can be deleted from a structure array in a similar manner to a | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
348 numerical array, by assigning the elements to an empty matrix. For |
6837 | 349 example |
350 | |
351 @example | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
352 @group |
7031 | 353 in = struct ("call1", @{x, Inf, "last"@}, |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
354 "call2", @{x, Inf, "first"@}) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
355 @result{} in = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
356 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
357 1x3 struct array containing the fields: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
358 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
359 call1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
360 call2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
361 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
362 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
363 in(1) = []; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
364 in.call1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
365 @result{} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
366 ans = Inf |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
367 ans = last |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
368 @end group |
6837 | 369 @end example |
370 | |
371 @node Creating Structures | |
372 @subsection Creating Structures | |
373 | |
374 As well as indexing a structure with ".", Octave can create a structure | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
375 with the @code{struct} command. @code{struct} takes pairs of arguments, |
6837 | 376 where the first argument in the pair is the fieldname to include in the |
377 structure and the second is a scalar or cell array, representing the | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
378 values to include in the structure or structure array. For example: |
6837 | 379 |
380 @example | |
381 @group | |
7031 | 382 struct ("field1", 1, "field2", 2) |
6837 | 383 @result{} ans = |
384 @{ | |
385 field1 = 1 | |
386 field2 = 2 | |
387 @} | |
388 @end group | |
389 @end example | |
390 | |
391 If the values passed to @code{struct} are a mix of scalar and cell | |
392 arrays, then the scalar arguments are expanded to create a | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
393 structure array with a consistent dimension. For example: |
6837 | 394 |
395 @example | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
396 @group |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
397 s = struct ("field1", @{1, "one"@}, "field2", @{2, "two"@}, |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
398 "field3", 3); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
399 s.field1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
400 @result{} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
401 ans = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
402 ans = one |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
403 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
404 s.field2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
405 @result{} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
406 ans = 2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
407 ans = two |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
408 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
409 s.field3 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
410 @result{} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
411 ans = 3 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
412 ans = 3 |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
413 @end group |
6837 | 414 @end example |
415 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
416 If you want to create a struct which contains a cell array as an |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
417 individual field, you have to put it into another cell array like in |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
418 the following example: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
419 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
420 @example |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
421 @group |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
422 struct ("field1", @{@{1, "one"@}@}, "field2", 2) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
423 @result{} ans = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
424 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
425 field1 = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
426 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
427 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
428 [1,1] = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
429 [1,2] = one |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
430 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
431 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
432 field2 = 2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
433 @} |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
434 @end group |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
435 @end example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
436 |
6837 | 437 @DOCSTRING(struct) |
438 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
439 The function @code{isstruct} can be used to test if an object is a |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
440 structure or a structure array. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
441 |
6837 | 442 @DOCSTRING(isstruct) |
443 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
444 @node Manipulating Structures |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
445 @subsection Manipulating Structures |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
446 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
447 Other functions that can manipulate the fields of a structure are given below. |
6837 | 448 |
11537
a4b0e292107f
Add undocumented function nfields to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
449 @DOCSTRING(nfields) |
6837 | 450 |
451 @DOCSTRING(fieldnames) | |
452 | |
453 @DOCSTRING(isfield) | |
454 | |
455 @DOCSTRING(getfield) | |
456 | |
11537
a4b0e292107f
Add undocumented function nfields to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
457 @DOCSTRING(setfield) |
a4b0e292107f
Add undocumented function nfields to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
458 |
a4b0e292107f
Add undocumented function nfields to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
459 @DOCSTRING(rmfield) |
a4b0e292107f
Add undocumented function nfields to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
460 |
a4b0e292107f
Add undocumented function nfields to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
461 @DOCSTRING(orderfields) |
a4b0e292107f
Add undocumented function nfields to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
462 |
6863 | 463 @DOCSTRING(substruct) |
464 | |
465 @node Processing Data in Structures | |
466 @subsection Processing Data in Structures | |
6837 | 467 |
6939 | 468 The simplest way to process data in a structure is within a @code{for} |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
469 loop (@pxref{Looping Over Structure Elements}). A similar effect can be |
6863 | 470 achieved with the @code{structfun} function, where a user defined |
6939 | 471 function is applied to each field of the structure. |
6863 | 472 |
473 @DOCSTRING(structfun) | |
474 | |
6939 | 475 Alternatively, to process the data in a structure, the structure might |
6863 | 476 be converted to another type of container before being treated. |
477 | |
478 @DOCSTRING(struct2cell) | |
6837 | 479 |
6517 | 480 @node Cell Arrays |
6837 | 481 @section Cell Arrays |
3439 | 482 @cindex cell arrays |
483 | |
6514 | 484 It can be both necessary and convenient to store several variables of |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
485 different size or type in one variable. A cell array is a container |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
486 class able to do just that. In general cell arrays work just like |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
487 @math{N}-dimensional arrays with the exception of the use of @samp{@{} |
6514 | 488 and @samp{@}} as allocation and indexing operators. |
489 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
490 @menu |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
491 * Basic Usage of Cell Arrays:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
492 * Creating Cell Arrays:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
493 * Indexing Cell Arrays:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
494 * Cell Arrays of Strings:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
495 * Processing Data in Cell Arrays:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
496 @end menu |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
497 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
498 @node Basic Usage of Cell Arrays |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
499 @subsection Basic Usage of Cell Arrays |
12175
2090995ca588
Correct en-dash,em-dash instances in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11537
diff
changeset
|
500 @opindex @{ |
2090995ca588
Correct en-dash,em-dash instances in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11537
diff
changeset
|
501 @opindex @} |
6514 | 502 As an example, the following code creates a cell array containing a |
503 string and a 2-by-2 random matrix | |
504 | |
505 @example | |
506 c = @{"a string", rand(2, 2)@}; | |
507 @end example | |
508 | |
509 @noindent | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
510 To access the elements of a cell array, it can be indexed with the @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
511 and @} operators. Thus, the variable created in the previous example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
512 can be indexed like this: |
6514 | 513 |
514 @example | |
515 @group | |
516 c@{1@} | |
517 @result{} ans = a string | |
518 @end group | |
519 @end example | |
520 | |
521 @noindent | |
522 As with numerical arrays several elements of a cell array can be | |
523 extracted by indexing with a vector of indexes | |
524 | |
525 @example | |
526 @group | |
527 c@{1:2@} | |
528 @result{} ans = | |
529 | |
530 (, | |
531 [1] = a string | |
532 [2] = | |
533 | |
534 0.593993 0.627732 | |
535 0.377037 0.033643 | |
536 | |
537 ,) | |
538 @end group | |
539 @end example | |
540 | |
541 The indexing operators can also be used to insert or overwrite elements | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
542 of a cell array. The following code inserts the scalar 3 on the |
6514 | 543 third place of the previously created cell array |
544 | |
545 @example | |
546 @group | |
547 c@{3@} = 3 | |
548 @result{} c = | |
549 | |
550 @{ | |
551 [1,1] = a string | |
552 [1,2] = | |
553 | |
554 0.593993 0.627732 | |
555 0.377037 0.033643 | |
556 | |
557 [1,3] = 3 | |
558 @} | |
559 @end group | |
560 @end example | |
561 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
562 Details on indexing cell arrays are explained in @ref{Indexing Cell Arrays}. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
563 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
564 In general nested cell arrays are displayed hierarchically as in the |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
565 previous example. In some circumstances it makes sense to reference |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
566 them by their index, and this can be performed by the @code{celldisp} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
567 function. |
6868 | 568 |
569 @DOCSTRING(celldisp) | |
570 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
571 To test if an object is a cell array, use the @code{iscell} |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
572 function. For example: |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
573 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
574 @example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
575 @group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
576 iscell(c) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
577 @result{} ans = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
578 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
579 iscell(3) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
580 @result{} ans = 0 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
581 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
582 @end group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
583 @end example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
584 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
585 @DOCSTRING(iscell) |
6516 | 586 |
6514 | 587 @node Creating Cell Arrays |
6837 | 588 @subsection Creating Cell Array |
6514 | 589 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
590 The introductory example (@pxref{Basic Usage of Cell Arrays}) showed |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
591 how to create a cell array containing currently available variables. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
592 In many situations, however, it is useful to create a cell array and |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
593 then fill it with data. |
6514 | 594 |
595 The @code{cell} function returns a cell array of a given size, containing | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
596 empty matrices. This function is similar to the @code{zeros} |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
597 function for creating new numerical arrays. The following example creates |
6514 | 598 a 2-by-2 cell array containing empty matrices |
599 | |
600 @example | |
601 @group | |
602 c = cell(2,2) | |
603 @result{} c = | |
604 | |
605 @{ | |
606 [1,1] = [](0x0) | |
607 [2,1] = [](0x0) | |
608 [1,2] = [](0x0) | |
609 [2,2] = [](0x0) | |
610 @} | |
611 @end group | |
612 @end example | |
613 | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
614 Just like numerical arrays, cell arrays can be multi-dimensional. The |
6514 | 615 @code{cell} function accepts any number of positive integers to describe |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
616 the size of the returned cell array. It is also possible to set the size |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
617 of the cell array through a vector of positive integers. In the |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8176
diff
changeset
|
618 following example two cell arrays of equal size are created, and the size |
6514 | 619 of the first one is displayed |
620 | |
621 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
622 @group |
6514 | 623 c1 = cell(3, 4, 5); |
624 c2 = cell( [3, 4, 5] ); | |
625 size(c1) | |
626 @result{} ans = | |
627 3 4 5 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
628 @end group |
6514 | 629 @end example |
630 | |
631 @noindent | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
632 As can be seen, the @ref{doc-size, @code{size}} function also works |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
633 for cell arrays. As do other functions describing the size of an |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
634 object, such as @ref{doc-length, @code{length}}, @ref{doc-numel, |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
635 @code{numel}}, @ref{doc-rows, @code{rows}}, and @ref{doc-columns, |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
636 @code{columns}}. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
637 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
638 @DOCSTRING(cell) |
6514 | 639 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
640 As an alternative to creating empty cell arrays, and then filling them, it |
6514 | 641 is possible to convert numerical arrays into cell arrays using the |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9758
diff
changeset
|
642 @code{num2cell}, @code{mat2cell} and @code{cellslices} functions. |
6514 | 643 |
644 @DOCSTRING(num2cell) | |
645 | |
646 @DOCSTRING(mat2cell) | |
647 | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9758
diff
changeset
|
648 @DOCSTRING(cellslices) |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9758
diff
changeset
|
649 |
6514 | 650 @node Indexing Cell Arrays |
6837 | 651 @subsection Indexing Cell Arrays |
6514 | 652 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
653 As shown in @pxref{Basic Usage of Cell Arrays} elements can be |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
654 extracted from cell arrays using the @samp{@{} and @samp{@}} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
655 operators. If you want to extract or access subarrays which are still |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
656 cell arrays, you need to use the @samp{(} and @samp{)} operators. The |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
657 following example illustrates the difference: |
6514 | 658 |
659 @example | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
660 @group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
661 c = @{"1", "2", "3"; "a", "b", "c"; "4", "5", "6"@}; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
662 c@{2,3@} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
663 @result{} ans = c |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
664 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
665 c(2,3) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
666 @result{} ans = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
667 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
668 [1,1] = c |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
669 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
670 @end group |
6514 | 671 @end example |
672 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
673 @noindent So with @samp{@{@}} you access elements of a cell |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
674 array, while with @samp{()} you access a sub array of a cell |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
675 array. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
676 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
677 Using the @samp{(} and @samp{)} operators, indexing works for cell |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
678 arrays like for multi-dimensional arrays. As an example, all the rows |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
679 of the first and third column of a cell array can be set to @code{0} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
680 with the following command: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
681 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
682 @example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
683 @group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
684 c(:, [1, 3]) = @{0@} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
685 @result{} = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
686 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
687 [1,1] = 0 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
688 [2,1] = 0 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
689 [3,1] = 0 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
690 [1,2] = 2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
691 [2,2] = 10 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
692 [3,2] = 20 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
693 [1,3] = 0 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
694 [2,3] = 0 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
695 [3,3] = 0 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
696 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
697 @end group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
698 @end example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
699 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
700 Note, that the above can also be achieved like this: |
6514 | 701 |
702 @example | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
703 c(:, [1, 3]) = 0; |
6514 | 704 @end example |
705 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
706 @noindent Here, the scalar @samp{0} is automatically promoted to |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
707 cell array @samp{@{0@}} and then assigned to the subarray of @code{c}. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
708 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
709 To give another example for indexing cell arrays with @samp{()}, you |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
710 can exchange the first and the second row of a cell array as in the |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
711 following command: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
712 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
713 @example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
714 @group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
715 c = @{1, 2, 3; 4, 5, 6@}; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
716 c([1, 2], :) = c([2, 1], :) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
717 @result{} = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
718 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
719 [1,1] = 4 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
720 [2,1] = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
721 [1,2] = 5 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
722 [2,2] = 2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
723 [1,3] = 6 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
724 [2,3] = 3 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
725 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
726 @end group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
727 @end example |
6514 | 728 |
6518 | 729 Accessing multiple elements of a cell array with the @samp{@{} and |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
730 @samp{@}} operators will result in a comma-separated list of all the |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
731 requested elements (@pxref{Comma Separated Lists}). Using the |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
732 @samp{@{} and @samp{@}} operators the first two rows in the above |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
733 example can be swapped back like this: |
6837 | 734 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
735 @example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
736 @group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
737 [c@{[1,2], :@}] = deal(c@{[2, 1], :@}) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
738 @result{} = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
739 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
740 [1,1] = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
741 [2,1] = 4 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
742 [1,2] = 2 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
743 [2,2] = 5 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
744 [1,3] = 3 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
745 [2,3] = 6 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
746 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
747 @end group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
748 @end example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
749 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
750 As for struct arrays and numerical arrays, the empty matrix @samp{[]} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
751 can be used to delete elements from a cell array: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
752 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
753 @example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
754 @group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
755 x = @{"1", "2"; "3", "4"@}; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
756 x(1, :) = [] |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
757 @result{} x = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
758 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
759 [1,1] = 3 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
760 [1,2] = 4 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
761 @} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
762 @end group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
763 @end example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
764 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
765 The following example shows how to just remove the contents of cell |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
766 array elements but not delete the space for them: |
6837 | 767 |
768 @example | |
769 @group | |
7031 | 770 x = @{"1", "2"; "3", "4"@}; |
6839 | 771 x@{1, :@} = [] |
772 @result{} x = | |
773 @{ | |
6837 | 774 [1,1] = [](0x0) |
775 [2,1] = 3 | |
776 [1,2] = [](0x0) | |
777 [2,2] = 4 | |
6839 | 778 @} |
6837 | 779 @end group |
780 @end example | |
781 | |
782 @node Cell Arrays of Strings | |
783 @subsection Cell Arrays of Strings | |
784 | |
785 One common use of cell arrays is to store multiple strings in the same | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
786 variable. It is also possible to store multiple strings in a |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
787 character matrix by letting each row be a string. This, however, |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
788 introduces the problem that all strings must be of equal length. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
789 Therefore, it is recommended to use cell arrays to store multiple |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
790 strings. For cases, where the character matrix representation is required |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
791 for an operation, there are several functions that convert a cell |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
792 array of strings to a character array and back. @code{char} and |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
793 @code{strvcat} convert cell arrays to a character array |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
794 (@pxref{Concatenating Strings}), while the function @code{cellstr} |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
795 converts a character array to a cell array of strings: |
6837 | 796 |
797 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
798 @group |
6837 | 799 a = ["hello"; "world"]; |
800 c = cellstr (a) | |
801 @result{} c = | |
802 @{ | |
803 [1,1] = hello | |
804 [2,1] = world | |
805 @} | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
806 @end group |
6837 | 807 @end example |
808 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
809 @DOCSTRING(cellstr) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
810 |
8828 | 811 One further advantage of using cell arrays to store multiple strings is |
6837 | 812 that most functions for string manipulations included with Octave |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
813 support this representation. As an example, it is possible to compare |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
814 one string with many others using the @code{strcmp} function. If one of |
6837 | 815 the arguments to this function is a string and the other is a cell array |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
816 of strings, each element of the cell array will be compared to the string |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
817 argument: |
6837 | 818 |
819 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
820 @group |
6837 | 821 c = @{"hello", "world"@}; |
822 strcmp ("hello", c) | |
823 @result{} ans = | |
824 1 0 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
825 @end group |
6837 | 826 @end example |
827 | |
828 @noindent | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
829 The following string functions support cell arrays of strings: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
830 @code{char}, @code{strvcat}, @code{strcat} (@pxref{Concatenating |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
831 Strings}), @code{strcmp}, @code{strncmp}, @code{strcmpi}, |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
832 @code{strncmpi} (@pxref{Comparing Strings}), @code{str2double}, |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
833 @code{deblank}, @code{strtrim}, @code{strtrunc}, @code{strfind}, |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
834 @code{strmatch}, , @code{regexp}, @code{regexpi} (@pxref{Manipulating |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
835 Strings}) and @code{str2double} (@pxref{String Conversions}). |
6837 | 836 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
837 The function @code{iscellstr} can be used to test if an object is a |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
838 cell array of strings. |
6837 | 839 |
840 @DOCSTRING(iscellstr) | |
841 | |
842 @node Processing Data in Cell Arrays | |
843 @subsection Processing Data in Cell Arrays | |
844 | |
845 Data that is stored in a cell array can be processed in several ways | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
846 depending on the actual data. The simplest way to process that data |
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
847 is to iterate through it using one or more @code{for} loops. The same |
8828 | 848 idea can be implemented more easily through the use of the @code{cellfun} |
8493
2f5a2c58c15e
[docs] user specified => user-specified
Brian Gough <bjg@gnu.org>
parents:
8372
diff
changeset
|
849 function that calls a user-specified function on all elements of a cell |
6837 | 850 array. |
851 | |
852 @DOCSTRING(cellfun) | |
853 | |
854 An alternative is to convert the data to a different container, such as | |
855 a matrix or a data structure. Depending on the data this is possible | |
856 using the @code{cell2mat} and @code{cell2struct} functions. | |
857 | |
858 @DOCSTRING(cell2mat) | |
859 | |
860 @DOCSTRING(cell2struct) | |
861 | |
862 @node Comma Separated Lists | |
863 @section Comma Separated Lists | |
864 @cindex comma separated lists | |
8541
d2cf983bdc9d
container.txi: define cs-list
John W. Eaton <jwe@octave.org>
parents:
8493
diff
changeset
|
865 @cindex cs-lists |
6837 | 866 |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
867 Comma separated lists @footnote{Comma-separated lists are also sometimes |
8541
d2cf983bdc9d
container.txi: define cs-list
John W. Eaton <jwe@octave.org>
parents:
8493
diff
changeset
|
868 informally referred to as @dfn{cs-lists}.} are the basic argument type |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
869 to all Octave functions - both for input and return arguments. In the |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
870 example |
6837 | 871 |
872 @example | |
873 max (@var{a}, @var{b}) | |
874 @end example | |
875 | |
876 @noindent | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
877 @samp{@var{a}, @var{b}} is a comma separated list. Comma separated lists |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
878 can appear on both the right and left hand side of an assignment. For |
6837 | 879 example |
6514 | 880 |
881 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
882 @group |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
883 x = [1 0 1 0 0 1 1; 0 0 0 0 0 0 7]; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
884 [@var{i}, @var{j}] = find (@var{x}, 2, "last"); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
885 @end group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
886 @end example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
887 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
888 @noindent |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
889 Here, @samp{@var{x}, 2, "last"} is a comma separated list constituting |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
890 the input arguments of @code{find}. @code{find} returns a comma |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
891 separated list of output arguments which is assigned element by |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
892 element to the comma separated list @samp{@var{i}, @var{j}}. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
893 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
894 Another example of where comma separated lists are used is in the |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
895 creation of a new array with @code{[]} (@pxref{Matrices}) or the |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
896 creation of a cell array with @code{@{@}} (@pxref{Basic Usage of Cell |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
897 Arrays}). In the expressions |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
898 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
899 @example |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
900 @group |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
901 a = [1, 2, 3, 4]; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
902 c = @{4, 5, 6, 7@}; |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
903 @end group |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
904 @end example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
905 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
906 @noindent |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
907 both @samp{1, 2, 3, 4} and @samp{4, 5, 6, 7} are comma separated lists. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
908 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
909 Comma separated lists cannot be directly manipulated by the |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
910 user. However, both structure arrays and cell arrays can be converted |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
911 into comma separated lists, and thus used in place of explicitly |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
912 written comma separated lists. This feature is useful in many ways, |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
913 as will be shown in the following subsections. |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
914 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
915 @menu |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
916 * Comma Separated Lists Generated from Cell Arrays:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
917 * Comma Separated Lists Generated from Structure Arrays:: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
918 @end menu |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
919 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
920 @node Comma Separated Lists Generated from Cell Arrays |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
921 @subsection Comma Separated Lists Generated from Cell Arrays |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
922 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
923 As has been mentioned above (@pxref{Indexing Cell Arrays}), elements |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
924 of a cell array can be extracted into a comma separated list with the |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
925 @code{@{} and @code{@}} operators. By surrounding this list with |
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9312
diff
changeset
|
926 @code{[} and @code{]}, it can be concatenated into an array. For example: |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
927 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
928 @example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
929 @group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
930 a = @{1, [2, 3], 4, 5, 6@}; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
931 b = [a@{1:4@}] |
6514 | 932 @result{} b = |
933 1 2 3 4 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
934 @end group |
6514 | 935 @end example |
936 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
937 Similarly, it is possible to create a new cell array containing cell |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
938 elements selected with @code{@{@}}. By surrounding the list with |
8828 | 939 @samp{@{} and @samp{@}} a new cell array will be created, as the |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
940 following example illustrates: |
6514 | 941 |
942 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
943 @group |
6514 | 944 a = @{1, rand(2, 2), "three"@}; |
945 b = @{ a@{ [1, 3] @} @} | |
946 @result{} b = | |
947 @{ | |
948 [1,1] = 1 | |
949 [1,2] = three | |
950 @} | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
951 @end group |
6514 | 952 @end example |
953 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
954 Furthermore, cell elements (accessed by @code{@{@}}) can be passed |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
955 directly to a function. The list of elements from the cell array will |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
956 be passed as an argument list to a given function as if it is called |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
957 with the elements as individual arguments. The two calls to |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
958 @code{printf} in the following example are identical but the latter is |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
959 simpler and can handle cell arrays of an arbitrary size: |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
960 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
961 @example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
962 @group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
963 c = @{"GNU", "Octave", "is", "Free", "Software"@}; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
964 printf ("%s ", c@{1@}, c@{2@}, c@{3@}, c@{4@}, c@{5@}); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
965 @print{} GNU Octave is Free Software |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
966 printf ("%s ", c@{:@}); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
967 @print{} GNU Octave is Free Software |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
968 @end group |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
969 @end example |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
970 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
971 If used on the left-hand side of an assignment, a comma separated list |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
972 generated with @code{@{@}} can be assigned to. An example is |
6514 | 973 |
974 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
975 @group |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
976 in@{1@} = [10, 20, 30, 40, 50, 60, 70, 80, 90]; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
977 in@{2@} = inf; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
978 in@{3@} = "last"; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
979 in@{4@} = "first"; |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
980 out = cell (4, 1); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
981 [out@{1:3@}] = find (in@{1 : 3@}); |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
982 [out@{4:6@}] = find (in@{[1, 2, 4]@}) |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
983 @result{} out = |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
984 @{ |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
985 [1,1] = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
986 [2,1] = 9 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
987 [3,1] = 90 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
988 [4,1] = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
989 [3,1] = 1 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
990 [4,1] = 10 |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
991 @} |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
992 @end group |
6514 | 993 @end example |
994 | |
995 | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
996 @node Comma Separated Lists Generated from Structure Arrays |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
997 @subsection Comma Separated Lists Generated from Structure Arrays |
6939 | 998 Structure arrays can equally be used to create comma separated |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
999 lists. This is done by addressing one of the fields of a structure |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10122
diff
changeset
|
1000 array. For example: |
6514 | 1001 |
6837 | 1002 @example |
1003 @group | |
1004 x = ceil (randn (10, 1)); | |
9312
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
1005 in = struct ("call1", @{x, 3, "last"@}, |
0f7480d07f6a
update documentation of container types in container.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9209
diff
changeset
|
1006 "call2", @{x, inf, "first"@}); |
7031 | 1007 out = struct ("call1", cell (2, 1), "call2", cell (2, 1)); |
6837 | 1008 [out.call1] = find (in.call1); |
1009 [out.call2] = find (in.call2); | |
1010 @end group | |
1011 @end example |