Main Content

struct2cell

Convert structure to cell array

Description

example

C = struct2cell(S) converts a structure into a cell array. The cell array C contains values copied from the fields of S.

The struct2cell function does not return field names. To return the field names in a cell array, use the fieldnames function.

Examples

collapse all

Create a structure.

S.x = linspace(0,2*pi);
S.y = sin(S.x);
S.title = 'y = sin(x)'
S = struct with fields:
        x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 ... ]
        y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 ... ]
    title: 'y = sin(x)'

Convert S to a cell array.

C = struct2cell(S)
C=3×1 cell array
    {[0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 ... ]}
    {[0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 ... ]}
    {'y = sin(x)'                                                           }

The cell array does not include field names. To return the field names in a cell array, use the fieldnames function. fieldnames and struct2cell return the field names and the values in the same order.

fields = fieldnames(S)
fields = 3x1 cell
    {'x'    }
    {'y'    }
    {'title'}

Input Arguments

collapse all

Input structure array. S can be a structure array of any size. If S is an m-by-n structure array with p fields, then C is a p-by-m-by-n cell array.

Extended Capabilities

Introduced before R2006a