annotate doc/interpreter/container.txi @ 6516:ce5caa673642

[project @ 2007-04-11 14:26:39 by jwe]
author jwe
date Wed, 11 Apr 2007 14:26:39 +0000
parents 5ef6f71974db
children a1ec359aef37
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3439
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
1 @c Copyright (C) 1996, 1997 John W. Eaton
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
2 @c This is part of the Octave manual.
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
3 @c For copying conditions, see the file gpl.texi.
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
4
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
5 @node Containers
3439
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
6 @chapter Containers
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
7 @cindex containers
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
8
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
9 @menu
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
10 * Cell Arrays::
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
11 @end menu
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
12
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 4029
diff changeset
13 @node Cell Arrays
3439
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
14 @section Cell Arrays
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
15 @cindex cell arrays
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
16
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
17 It can be both necessary and convenient to store several variables of
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
18 different size or type in one variable. A cell array is a container
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
19 class able to do just that. In general cell arrays work just like
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
20 @math{N}-dimensional arrays, with the exception of the use of @samp{@{}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
21 and @samp{@}} as allocation and indexing operators.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
22
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
23 As an example, the following code creates a cell array containing a
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
24 string and a 2-by-2 random matrix
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
25
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
26 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
27 c = @{"a string", rand(2, 2)@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
28 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
29
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
30 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
31 And a cell array can be indexed with the @{ and @} operators, so the
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
32 variable created in the previous example can be indexed like this
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
33
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
34 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
35 @group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
36 c@{1@}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
37 @result{} ans = a string
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
38 @end group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
39 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
40
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
41 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
42 As with numerical arrays several elements of a cell array can be
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
43 extracted by indexing with a vector of indexes
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
44
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
45 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
46 @group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
47 c@{1:2@}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
48 @result{} ans =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
49
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
50 (,
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
51 [1] = a string
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
52 [2] =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
53
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
54 0.593993 0.627732
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
55 0.377037 0.033643
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
56
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
57 ,)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
58 @end group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
59 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
60
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
61 The indexing operators can also be used to insert or overwrite elements
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
62 of a cell array. The following code inserts the scalar 3 on the
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
63 third place of the previously created cell array
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
64
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
65 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
66 @group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
67 c@{3@} = 3
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
68 @result{} c =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
69
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
70 @{
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
71 [1,1] = a string
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
72 [1,2] =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
73
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
74 0.593993 0.627732
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
75 0.377037 0.033643
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
76
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
77 [1,3] = 3
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
78 @}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
79 @end group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
80 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
81
6516
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
82 @menu
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
83 * Creating Cell Arrays::
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
84 * Indexing Cell Arrays::
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
85 * Cell Arrays of Strings::
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
86 * Processing Data in Cell Arrays::
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
87 @end menu
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
88
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
89 @node Creating Cell Arrays
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
90 @subsection Creating Cell Array
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
91
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
92 The introductory example showed how to create a cell array containing
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
93 currently available variables. In many situations, however, it is useful
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
94 to create a cell array and then fill it with data.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
95
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
96 The @code{cell} function returns a cell array of a given size, containing
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
97 empty matrices. This function works very similar to the @code{zeros}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
98 function for creating new numerical arrays. The following example creates
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
99 a 2-by-2 cell array containing empty matrices
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
100
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
101 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
102 @group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
103 c = cell(2,2)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
104 @result{} c =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
105
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
106 @{
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
107 [1,1] = [](0x0)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
108 [2,1] = [](0x0)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
109 [1,2] = [](0x0)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
110 [2,2] = [](0x0)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
111 @}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
112 @end group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
113 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
114
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
115 Just like numerical arrays, cell arrays can be multidimensional. The
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
116 @code{cell} function accepts any number of positive integers to describe
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
117 the size of the returned cell array. It is also possible to set the size
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
118 of the cell array through a vector of positive integers. In the
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
119 following example two cell arrays of equal size is created, and the size
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
120 of the first one is displayed
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
121
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
122 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
123 c1 = cell(3, 4, 5);
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
124 c2 = cell( [3, 4, 5] );
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
125 size(c1)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
126 @result{} ans =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
127 3 4 5
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
128 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
129
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
130 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
131 As can be seen, the @code{size} function also work for cell arrays. As
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
132 do the other functions describing the size of an object, such as
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
133 @code{length}, @code{numel}, @code{rows}, and @code{columns}.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
134
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
135 An alternative to creating empty cell arrays, and then filling them, it
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
136 is possible to convert numerical arrays into cell arrays using the
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
137 @code{num2cell} and @code{mat2cell} functions.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
138
3439
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
139 @DOCSTRING(cell)
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
140
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
141 @DOCSTRING(iscell)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
142
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
143 @DOCSTRING(num2cell)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
144
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
145 @DOCSTRING(mat2cell)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
146
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
147 @node Indexing Cell Arrays
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
148 @subsection Indexing Cell Arrays
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
149
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
150 As shown in the introductory example elements can be inserted from cell
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
151 arrays using the @samp{@{} and @samp{@}} operators. Besides the change
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
152 of operators, indexing works for cell arrays like for multidimensional
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
153 arrays. As an example, all the rows of the first and third column of a
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
154 cell array can be set to @code{0} with the following code
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
155
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
156 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
157 c@{:, [1, 3]@} = 0;
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
158 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
159
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
160 Accessing values in a cell array is, however, different from the same
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
161 operation for numerical arrays. Accessing a single element of a cell
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
162 array is very similar to numerical arrays, for example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
163
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
164 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
165 element = c@{1, 2@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
166 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
167
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
168 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
169 This will, however, @emph{not} work when accessing multiple elements of
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
170 a cell array, because it might not be possible to represent all elements
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
171 with a single variable as is the case with numerical arrays.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
172
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
173 Accessing multiple elements of a cell array will result in a list of all
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
174 the requested elements. This list can then form the basis of a new
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
175 numerical array or cell array, or be passed as arguments to a
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
176 function. If all the accessed elements of a cell array are scalars or
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
177 column vectors, they can be concatenated into a new column vector
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
178 containing the elements, by surrounding the list with @code{[} and
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
179 @code{]} as in the following example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
180
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
181 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
182 a = @{1, [2, 3], 4@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
183 b = [a@{:@}]
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
184 @result{} b =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
185 1 2 3 4
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
186 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
187
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
188 In much the same way, a new cell array containing the accessed elements
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
189 can be created. By surrounding the list with @samp{@{} and @samp{@}} a
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
190 new cell array will be created, like the following example illustrates
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
191
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
192 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
193 a = @{1, rand(2, 2), "three"@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
194 b = @{ a@{ [1, 3] @} @}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
195 @result{} b =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
196 @{
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
197 [1,1] = 1
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
198 [1,2] = three
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
199 @}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
200 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
201
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
202 It is also possible to pass the accessed elements directly to a
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
203 function. The list of elements from the cell array will be passed as an
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
204 argument list to a given function if it is called with the elements as
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
205 arguments. The two calls to @code{printf} in the following example are
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
206 identical but the latter is more simple and handles more situations
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
207
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
208 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
209 c = @{"GNU", "Octave", "is", "Free", "Software"@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
210 printf ("%s ", c@{1@}, c@{2@}, c@{3@}, c@{4@}, c@{5@});
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
211 @print{} GNU Octave is Free Software
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
212 printf ("%s ", c@{:@});
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
213 @print{} GNU Octave is Free Software
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
214 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
215
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
216 @node Cell Arrays of Strings
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
217 @subsection Cell Arrays of Strings
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
218
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
219 One common use of cell arrays is to store multiple strings in the same
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
220 variable. It is possible to store multiple strings in a character matrix
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
221 by letting each row be a string. This, however, introduces the problem
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
222 that all strings must be of equal length. Therefore it is recommended to
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
223 use cell arrays to store multiple strings. If, however, the character
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
224 matrix representation is required for an operation, it can be converted
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
225 to a cell array of strings using the @code{cellstr} function
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
226
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
227 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
228 a = ["hello"; "world"];
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
229 c = cellstr (a)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
230 @result{} c =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
231 @{
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
232 [1,1] = hello
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
233 [2,1] = world
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
234 @}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
235 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
236
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
237 One further advantage of using cell arrays to store multiple strings, is
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
238 that most functions for string manipulations included with Octave
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
239 supports this representation. As an example, it is possible to compare
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
240 one string with many others using the @code{strcmp} function. If one of
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
241 the arguments to this function is a string and the other is a cell array
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
242 of strings, each element of the cell array will be compared the string
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
243 argument,
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
244
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
245 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
246 c = @{"hello", "world"@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
247 strcmp ("hello", c)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
248 @result{} ans =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
249 1 0
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
250 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
251
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
252 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
253 The following functions for string manipulation support cell arrays of
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
254 strings, @code{strcmp}, @code{strcmpi}, @code{strncmp}, @code{strncmpi},
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
255 @code{str2double}, @code{str2mat}, @code{strappend}, @code{strtrunc},
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
256 @code{strvcat}, @code{strfind}, and @code{strmatch}.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
257
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4167
diff changeset
258 @DOCSTRING(cellstr)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4167
diff changeset
259
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
260 @DOCSTRING(iscellstr)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
261
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
262 @DOCSTRING(cellidx)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
263
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
264 @node Processing Data in Cell Arrays
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
265 @subsection Processing Data in Cell Arrays
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
266
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
267 Data that is stored in a cell array can be processed in several ways
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
268 depending on the actual data. The most simple way to process that data
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
269 is to iterate through it using one or more @code{for} loops. The same
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
270 idea can be implemented easier through the use of the @code{cellfun}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
271 function that calls a user specified function on all elements of a cell
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
272 array.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
273
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
274 @DOCSTRING(cellfun)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
275
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
276 An alternative is to convert the data to a different container, such as
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
277 a matrix or a data structure. Depending on the data this is possible
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
278 using the @code{cell2mat} and @code{cell2struct} functions.
6502
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4358
diff changeset
279
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4358
diff changeset
280 @DOCSTRING(cell2mat)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4358
diff changeset
281
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4358
diff changeset
282 @DOCSTRING(cell2struct)