cell2struct - Convert cell array to structure array

Syntax

s = cell2struct(c, fields, dim)

Description

s = cell2struct(c, fields, dim) creates a structure array s from the information contained within cell array c.

The fields argument specifies field names for the structure array. fields can be a character array or a cell array of strings.

The dim argument controls which axis of the cell array is to be used in creating the structure array. The length of c along the specified dimension must match the number of fields named in fields. In other words, the following must be true.

size(c,dim) == length(fields)   % If fields is a cell array
size(c,dim) == size(fields,1)   % If fields is a char array

Examples

The cell array c in this example contains information on trees. The three columns of the array indicate the common name, genus, and average height of a tree.

c = {'birch', 'betula', 65;  'maple', 'acer', 50}
c = 
    'birch'    'betula'    [65]
    'maple'    'acer'      [50]

To put this information into a structure with the fields name, genus, and height, use cell2struct along the second dimension of the 2-by-3 cell array.

fields = {'name', 'genus', 'height'};
s = cell2struct(c, fields, 2);

This yields the following 2-by-1 structure array.

s(1)                        s(2)
ans =                       ans =
      name: 'birch'               name: 'maple'
     genus: 'betula'             genus: 'acer'
    height: 65                  height: 50

See Also

struct2cell, cell, iscell, struct, isstruct, fieldnames, dynamic field names

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS