Main Content

cell2struct

Convert cell array to structure array

    Description

    structArray = cell2struct(cellArray,fields) creates a structure array from the information contained in the cell array and using the specified field names. The function assigns each row of the cell array to the corresponding field name in the structure.

    example

    structArray = cell2struct(cellArray,fields,dim) assigns the data sets along the dimension dim of the cell array to the corresponding field name in the structure. For example, specify 2 for dim to assign each column of the cell array to the corresponding field name.

    example

    Examples

    collapse all

    Create a cell array that stores high and low temperatures for five days.

    T(1,:) = {"highs",[44,38,46,43,48]};
    T(2,:) = {"lows",[35,31,32,28,35]}
    T=2×2 cell array
        {["highs"]}    {[44 38 46 43 48]}
        {["lows" ]}    {[35 31 32 28 35]}
    
    

    Use the first column of data in the cell array to define field names for the structure.

    fields = [T{:,1}]
    fields = 1x2 string
        "highs"    "lows"
    
    

    Copy the temperature data from the second column of the cell array into a cell array named temps.

    temps = T(:,2)
    temps=2×1 cell array
        {[44 38 46 43 48]}
        {[35 31 32 28 35]}
    
    

    Call cell2struct with the temps cell array and field names as inputs. MATLAB assigns each row of data from temps to the corresponding field name.

    Tstruct = cell2struct(temps,fields)
    Tstruct = struct with fields:
        highs: [44 38 46 43 48]
         lows: [35 31 32 28 35]
    
    

    Index into the structure to get the low temperature for the third day.

    Tstruct.lows(3)
    ans = 
    32
    

    Create a cell array that contains anonymous patient information, including an ID number, body temperature, and blood pressure.

    patients(1,:) = {"A134",98.5,[124 85]};
    patients(2,:) = {"B267",99.1,[109 77]};
    patients(3,:) = {"C889",97.9,[120 81]}
    patients=3×3 cell array
        {["A134"]}    {[98.5000]}    {[124 85]}
        {["B267"]}    {[99.1000]}    {[109 77]}
        {["C889"]}    {[97.9000]}    {[120 81]}
    
    

    Create a vector of field names to use for the struct.

    fields = ["PatientID","BodyTemp","BloodPressure"];

    To associate each column of data in the cell array with the corresponding field name in the structure, call cell2struct with the dimension set to 2.

    patientStruct = cell2struct(patients,fields,2)
    patientStruct=3×1 struct array with fields:
        PatientID
        BodyTemp
        BloodPressure
    
    

    Retrieve the blood pressure of patient C889 in the newly created structure array.

    patientC889BP = patientStruct(3).BloodPressure
    patientC889BP = 1×2
    
       120    81
    
    

    Input Arguments

    collapse all

    The cell array to be converted to a structure array.

    The names of the structure array fields, specified as a string array or cell array of character vectors. The number of field names must match the number of the number of cells along the dimension specified by dim.

    The dimension along which to split the cell array, specified as a positive integer. For example, the default value of 1 assigns each row of the cell array to the corresponding field name in fields. Specify 2 to assign each column of the cell array to the corresponding field name.

    Extended Capabilities

    Version History

    Introduced before R2006a