Documentation Center

  • Trial Software
  • Product Updates

table2struct

Convert table to structure array

Syntax

  • S = table2struct(T) example
  • S = table2struct(T,'ToScalar',true) example

Description

example

S = table2struct(T) converts the table, T, to a structure array, S. Each variable of T becomes a field in S. If T is an m-by-n table, then S is a m-by-1 structure array with n fields.

example

S = table2struct(T,'ToScalar',true) converts the table, T, to a scalar structure S. Each variable of T becomes a field in S. If T is a m-by-n table, then S has n fields, each of which has m rows.

Examples

expand all

Convert Table to Structure Array

Create a table, T, with five rows and three variables.

T = table(['M';'M';'F';'F';'F'],[38;43;38;40;49],...
    [124 93;109 77; 125 83; 117 75; 122 80],...
    'VariableNames',{'Gender' 'Age' 'BloodPressure'})
T = 

    Gender    Age     BloodPressure 
    ______    ___    _______________

    M         38     124          93
    M         43     109          77
    F         38     125          83
    F         40     117          75
    F         49     122          80

Convert T to a structure array.

S = table2struct(T)
S = 

5x1 struct array with fields:

    Gender
    Age
    BloodPressure

The structure is 5-by-1, corresponding to the five rows of the table, T. The three fields of S correspond to the three variables from T.

Display the field data for the first element of S.

S(1)
ans = 

           Gender: 'M'
              Age: 38
    BloodPressure: [124 93]

The information corresponds to the first row of the table.

Convert Table to Scalar Structure

Create a table, T, with five rows and three variables.

T = table(['M';'M';'F';'F';'F'],[38;43;38;40;49],...
    [124 93;109 77; 125 83; 117 75; 122 80],...
    'VariableNames',{'Gender' 'Age' 'BloodPressure'})
T = 

    Gender    Age     BloodPressure 
    ______    ___    _______________

    M         38     124          93
    M         43     109          77
    F         38     125          83
    F         40     117          75
    F         49     122          80

Convert T to a scalar structure.

S = table2struct(T,'ToScalar',true)
S = 

           Gender: [5x1 char]
              Age: [5x1 double]
    BloodPressure: [5x2 double]

The data in the fields of the scalar structure are 5-by-1, corresponding to the five rows in the table T.

Display the data for the field BloodPressure.

S.BloodPressure
ans =

   124    93
   109    77
   125    83
   117    75
   122    80

The structure field BloodPressure contains all of the data that was in the variable of the same name from table T.

Convert Table with Row Names to Structure

Create a table, T, that includes row names.

T = table(['M';'M';'F';'F';'F'],[38;43;38;40;49],...
    [124 93;109 77; 125 83; 117 75; 122 80],...
    'VariableNames',{'Gender' 'Age' 'BloodPressure'},...
    'RowNames',{'Smith' 'Johnson' 'Williams' 'Jones' 'Brown'})
T = 

                Gender    Age     BloodPressure 
                ______    ___    _______________

    Smith       M         38     124          93
    Johnson     M         43     109          77
    Williams    F         38     125          83
    Jones       F         40     117          75
    Brown       F         49     122          80

Convert T to a scalar structure.

S = table2struct(T,'ToScalar',true)
S = 

           Gender: [5x1 char]
              Age: [5x1 double]
    BloodPressure: [5x2 double]

Add a field for the row names from the table.

S.RowNames = T.Properties.RowNames
S = 

           Gender: [5x1 char]
              Age: [5x1 double]
    BloodPressure: [5x2 double]
         RowNames: {5x1 cell}

If S is a nonscalar structure, use [S.RowNames] = T.Properties.RowNames{:} to include a field with the row names from the table.

Input Arguments

expand all

T — Input tabletable

Input table, specified as a table.

See Also

| | |

Was this topic helpful?