6.3.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.

: 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.

: S = cell2struct (cell, fields)
: S = 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.

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

See also: struct2cell, cell2mat, struct.