GNU Octave: Processing Data in Cell Arrays

Previous: Cell Arrays of Strings, Up: Cell Arrays [Contents][Index]

#### 6.2.5 Processing Data in Cell Arrays

Data that is stored in a cell array can be processed in several ways depending on the actual data. The simplest way to process that data is to iterate through it using one or more `for` loops. The same idea can be implemented more easily through the use of the `cellfun` function that calls a user-specified function on all elements of a cell array. See cellfun.

An alternative is to convert the data to a different container, such as a matrix or a data structure. Depending on the data this is possible using the `cell2mat` and `cell2struct` functions.

Function File: m = cell2mat (c)

  Convert the cell array c into a matrix by concatenating all elements of c into a hyperrectangle.

  Elements of c must be numeric, logical, or char matrices; or cell arrays; or structs; and `cat` must be able to concatenate them together.

  See also: mat2cell, num2cell.

Built-in Function: cell2struct (cell, fields)
Built-in Function: cell2struct (cell, fields, dim)

  Convert cell to a structure.

  The number of fields in fields must match the number of elements in cell along dimension dim, that is `numel (fields) == size (cell, dim)`. If dim is omitted, a value of 1 is assumed.

      A = cell2struct ({"Peter", "Hannah", "Robert";
                         185, 170, 168},
                       {"Name","Height"}, 1);
      A(1)
         ⇒
            {
              Name   = Peter
              Height = 185
            }

  See also: struct2cell, cell2mat, struct.