Categories &

Functions List

Function Reference: struct2table

datatypes: tbl = struct2table (S)
datatypes: tbl = struct2table (S, Name, Value)

Convert a structure array to a table.

tbl = struct2table (S) converts a structure array S to the table tbl, where each field of the input structure becomes a variable in the output table. For a scalar structure with N fields, all of which have M rows, or an M×1 or 1×M structure array with N fields, the output is an M×N table.

tbl = struct2table (S, Name, Value) specifies optional parameters for creating the table tbl with the following Name-Value paired arguments.

NameValue
'AsArray'A logical scalar specifying whether to treat a scalar input as a structure array, which allows the fields containing data of different sizes.
'RowNames'A cell array of character vectors or a string array defining the row names of tbl. The names must be unique but not necessarily valid variable names.
'DimensionNames'A cell array of character vectors or a string array defining the dimension names of tbl. The names must be unique and not in conflict with variable names. By default, dimension names are 'Row', 'Variables'.

Source Code: struct2table

See also: array2table, cell2table, table

Source Code: struct2table

Example: 1

struct2table maps a struct to a table. A struct array becomes one row per element; a scalar struct whose fields are equal-length columns becomes one row per element of those columns. Both give the same table here.

 S = struct ('Name', {'Li'; 'Diaz'; 'Brown'}, 'Age', {38; 40; 49});
 struct2table (S)
ans =
  3x2 table

      Name       Age    
    _________    ___    

    {'Li'   }     38    
    {'Diaz' }     40    
    {'Brown'}     49

Example: 2

When a scalar struct holds fields of unequal length, use 'AsArray', true to wrap the whole struct as a single table row, each field one variable.

 S.Values = [1, 2, 3];
 S.Label = 'demo';
 struct2table (S, 'AsArray', true)
ans =
  1x2 table

      Values       Label     
    ___________    ______    

    1    2    3    'demo'