- publishing free software manuals
GNU Octave Manual Version 3
by John W. Eaton, David Bateman, Søren Hauberg
Paperback (6"x9"), 568 pages
ISBN 095461206X
RRP £24.95 ($39.95)

Get a printed copy>>>

6.2 Cell Arrays

Like structures, a cell array is able to store variables of different size or type. A cell array uses multi-dimensional integer indices, just like an ordinary N-dimensional numerical array. To distinguish them from ordinary arrays, cell arrays use braces ‘{’ and ‘}’ as allocation and indexing operators.

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)};

And a cell array can be indexed with the { and } operators, so 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 =
          
          (,
            [1] = a string
            [2] =
          
               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
         }

In general nested cell arrays are displayed hierarchically as above. In some circumstances it makes sense to reference them by their index, and this can be performed by the celldisp function.

Function File: celldisp (c, name)
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.

See also disp

ISBN 095461206XGNU Octave Manual Version 3See the print edition