As an example, the following code creates a cell array containing a string and a 2-by-2 random matrix
c = {"a string", rand(2, 2)};
To access the elements of a cell array, it can be indexed with the { and } operators. Thus, the variable created in the previous example can be indexed like this:
c{1} ⇒ ans = a string
As with numerical arrays several elements of a cell array can be extracted by indexing with a vector of indexes
c{1:2} ⇒ ans = a string ⇒ ans = 0.593993 0.627732 0.377037 0.033643
The indexing operators can also be used to insert or overwrite elements of a cell array. The following code inserts the scalar 3 on the third place of the previously created cell array
c{3} = 3 ⇒ c = { [1,1] = a string [1,2] = 0.593993 0.627732 0.377037 0.033643 [1,3] = 3 }
Details on indexing cell arrays are explained in Indexing Cell Arrays.
In general nested cell arrays are displayed hierarchically as in the previous example. In some circumstances it makes sense to reference them by their index, and this can be performed by the celldisp
function.
Recursively display the contents of a cell array.
By default the values are displayed with the name of the variable c. However, this name can be replaced with the variable name. For example:
c = {1, 2, {31, 32}}; celldisp (c, "b") ⇒ b{1} = 1 b{2} = 2 b{3}{1} = 31 b{3}{2} = 32
See also: disp.
To test if an object is a cell array, use the iscell
function. For example:
iscell (c) ⇒ ans = 1 iscell (3) ⇒ ans = 0
© 1996–2018 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/interpreter/Basic-Usage-of-Cell-Arrays.html