annotate doc/interpreter/container.txi @ 6778:083721ae3dfa

[project @ 2007-07-18 17:03:10 by jwe]
author jwe
date Wed, 18 Jul 2007 17:03:11 +0000
parents 545847da3b88
children 7eefeed173ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6778
083721ae3dfa [project @ 2007-07-18 17:03:10 by jwe]
jwe
parents: 6623
diff changeset
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton
3439
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
6517
a1ec359aef37 [project @ 2007-04-11 15:14:48 by jwe]
jwe
parents: 6516
diff changeset
5 @node Cell Arrays
a1ec359aef37 [project @ 2007-04-11 15:14:48 by jwe]
jwe
parents: 6516
diff changeset
6 @chapter Cell Arrays
3439
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 @cindex cell arrays
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
9
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
10 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
11 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
12 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
13 @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
14 and @samp{@}} as allocation and indexing operators.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
15
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
16 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
17 string and a 2-by-2 random matrix
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
18
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
19 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
20 c = @{"a string", rand(2, 2)@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
21 @end example
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 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
24 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
25 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
26
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
27 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
28 @group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
29 c@{1@}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
30 @result{} ans = a string
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
31 @end group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
32 @end example
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 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
35 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
36 extracted by indexing with a vector of indexes
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
37
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
38 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
39 @group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
40 c@{1:2@}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
41 @result{} ans =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
42
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
43 (,
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
44 [1] = a string
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
45 [2] =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
46
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
47 0.593993 0.627732
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
48 0.377037 0.033643
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 @end group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
52 @end example
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 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
55 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
56 third place of the previously created cell array
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 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
59 @group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
60 c@{3@} = 3
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
61 @result{} c =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
62
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
63 @{
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
64 [1,1] = a string
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
65 [1,2] =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
66
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
67 0.593993 0.627732
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
68 0.377037 0.033643
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 [1,3] = 3
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
71 @}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
72 @end group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
73 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
74
6516
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
75 @menu
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
76 * Creating Cell Arrays::
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
77 * Indexing Cell Arrays::
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
78 * Cell Arrays of Strings::
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
79 * Processing Data in Cell Arrays::
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
80 @end menu
ce5caa673642 [project @ 2007-04-11 14:26:39 by jwe]
jwe
parents: 6514
diff changeset
81
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
82 @node Creating Cell Arrays
6517
a1ec359aef37 [project @ 2007-04-11 15:14:48 by jwe]
jwe
parents: 6516
diff changeset
83 @section Creating Cell Array
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
84
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
85 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
86 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
87 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
88
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
89 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
90 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
91 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
92 a 2-by-2 cell array containing empty matrices
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
93
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
94 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
95 @group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
96 c = cell(2,2)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
97 @result{} c =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
98
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
99 @{
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
100 [1,1] = [](0x0)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
101 [2,1] = [](0x0)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
102 [1,2] = [](0x0)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
103 [2,2] = [](0x0)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
104 @}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
105 @end group
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
106 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
107
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
108 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
109 @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
110 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
111 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
112 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
113 of the first one is displayed
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 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
116 c1 = cell(3, 4, 5);
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
117 c2 = cell( [3, 4, 5] );
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
118 size(c1)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
119 @result{} ans =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
120 3 4 5
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
121 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
122
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
123 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
124 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
125 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
126 @code{length}, @code{numel}, @code{rows}, and @code{columns}.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
127
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
128 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
129 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
130 @code{num2cell} and @code{mat2cell} functions.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
131
3439
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
132 @DOCSTRING(cell)
3234a698073a [project @ 2000-01-14 09:51:14 by jwe]
jwe
parents:
diff changeset
133
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
134 @DOCSTRING(iscell)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
135
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
136 @DOCSTRING(num2cell)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
137
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
138 @DOCSTRING(mat2cell)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
139
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
140 @node Indexing Cell Arrays
6517
a1ec359aef37 [project @ 2007-04-11 15:14:48 by jwe]
jwe
parents: 6516
diff changeset
141 @section Indexing Cell Arrays
6514
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 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
144 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
145 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
146 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
147 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
148
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
149 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
150 c@{:, [1, 3]@} = 0;
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
151 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
152
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
153 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
154 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
155 array is very similar to numerical arrays, for example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
156
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
157 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
158 element = c@{1, 2@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
159 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
160
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
161 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
162 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
163 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
164 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
165
6518
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
166 Accessing multiple elements of a cell array with the @samp{@{} and
6623
545847da3b88 [project @ 2007-05-15 02:23:08 by jwe]
jwe
parents: 6518
diff changeset
167 @samp{@}} operators will result in a comma-separated list of all
6518
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
168 the requested elements. This list can then be used anywhere where a
6623
545847da3b88 [project @ 2007-05-15 02:23:08 by jwe]
jwe
parents: 6518
diff changeset
169 comma-separated list is used, such as in the creation of a new
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
170 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
171 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
172 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
173 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
174 @code{]} as in the following example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
175
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
176 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
177 a = @{1, [2, 3], 4@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
178 b = [a@{:@}]
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
179 @result{} b =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
180 1 2 3 4
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
181 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
182
6518
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
183 It is also possible to pass the accessed elements directly to a
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
184 function. The list of elements from the cell array will be passed as an
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
185 argument list to a given function as if it is called with the elements as
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
186 arguments. The two calls to @code{printf} in the following example are
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
187 identical but the latter is more simple and handles more situations
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
188
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
189 @example
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
190 c = @{"GNU", "Octave", "is", "Free", "Software"@};
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
191 printf ("%s ", c@{1@}, c@{2@}, c@{3@}, c@{4@}, c@{5@});
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
192 @print{} GNU Octave is Free Software
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
193 printf ("%s ", c@{:@});
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
194 @print{} GNU Octave is Free Software
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
195 @end example
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
196
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
197 Just like it is possible to create a numerical array from selected
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
198 elements of a cell array, it is possible to create a new cell array
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
199 containing the selected elements. By surrounding the list with
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
200 @samp{@{} and @samp{@}} a new cell array will be created, like the
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
201 following example illustrates
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
202
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
203 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
204 a = @{1, rand(2, 2), "three"@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
205 b = @{ a@{ [1, 3] @} @}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
206 @result{} b =
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 [1,1] = 1
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
209 [1,2] = three
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
210 @}
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
211 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
212
6518
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
213 @noindent
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
214 This syntax is however a bit cumbersome, and since this is a common
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
215 operation, it is possible to achieve the same using the @samp{(}
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
216 and @samp{)} operators for indexing. When a cell array is indexed
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
217 using the @samp{(} and @samp{)} operators a new cell array containing
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
218 the selected elements. Using this syntax, the previous example can
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
219 be simplified into the following
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
220
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
221 @example
6518
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
222 a = @{1, rand(2, 2), "three"@};
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
223 b = a( [1, 3] )
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
224 @result{} b =
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
225 @{
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
226 [1,1] = 1
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
227 [1,2] = three
952c8b00525e [project @ 2007-04-11 20:50:22 by jwe]
jwe
parents: 6517
diff changeset
228 @}
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
229 @end example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
230
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
231 @node Cell Arrays of Strings
6517
a1ec359aef37 [project @ 2007-04-11 15:14:48 by jwe]
jwe
parents: 6516
diff changeset
232 @section Cell Arrays of Strings
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
233
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
234 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
235 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
236 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
237 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
238 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
239 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
240 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
241
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
242 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
243 a = ["hello"; "world"];
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
244 c = cellstr (a)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
245 @result{} c =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
246 @{
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
247 [1,1] = hello
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
248 [2,1] = world
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
249 @}
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 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
253 that most functions for string manipulations included with Octave
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
254 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
255 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
256 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
257 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
258 argument,
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
259
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
260 @example
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
261 c = @{"hello", "world"@};
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
262 strcmp ("hello", c)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
263 @result{} ans =
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
264 1 0
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
265 @end example
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 @noindent
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
268 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
269 strings, @code{strcmp}, @code{strcmpi}, @code{strncmp}, @code{strncmpi},
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
270 @code{str2double}, @code{str2mat}, @code{strappend}, @code{strtrunc},
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
271 @code{strvcat}, @code{strfind}, and @code{strmatch}.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
272
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4167
diff changeset
273 @DOCSTRING(cellstr)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4167
diff changeset
274
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
275 @DOCSTRING(iscellstr)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
276
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
277 @DOCSTRING(cellidx)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
278
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
279 @node Processing Data in Cell Arrays
6517
a1ec359aef37 [project @ 2007-04-11 15:14:48 by jwe]
jwe
parents: 6516
diff changeset
280 @section Processing Data in Cell Arrays
6514
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
281
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
282 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
283 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
284 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
285 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
286 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
287 array.
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
288
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
289 @DOCSTRING(cellfun)
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
290
5ef6f71974db [project @ 2007-04-11 14:15:40 by jwe]
jwe
parents: 6502
diff changeset
291 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
292 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
293 using the @code{cell2mat} and @code{cell2struct} functions.
6502
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4358
diff changeset
294
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4358
diff changeset
295 @DOCSTRING(cell2mat)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4358
diff changeset
296
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4358
diff changeset
297 @DOCSTRING(cell2struct)