table2struct - Convert table to structure array - MATLAB (original) (raw)

Convert table to structure array

Syntax

Description

S = table2struct([T](#btx325k-1-T)) converts the table or timetable,T, to a structure array, S. Each variable in T becomes a field in S. IfT is an m-by-n table or timetable, then S is a m-by-1 structure array with n fields.

The output S does not include the table properties inT.Properties.

example

S = table2struct([T](#btx325k-1-T),"ToScalar",true) converts the table, T, to a scalar structure S. Each variable of T becomes a field in S. IfT is a m-by-n table, then S has n fields, each of which hasm rows.

example

Examples

collapse all

Convert Table to Structure Array

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

T = table(categorical(["Y";"N";"Y";"N";"N"]),[38;43;38;40;49],... [124 93;109 77; 125 83; 117 75; 122 80],... 'VariableNames',["Smoker" "Age" "BloodPressure"])

T=5×3 table Smoker Age BloodPressure ______ ___ _____________

  Y       38      124     93  
  N       43      109     77  
  Y       38      125     83  
  N       40      117     75  
  N       49      122     80  

Convert T to a structure array.

S=5×1 struct array with fields: Smoker 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.

ans = struct with fields: Smoker: Y 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(categorical(["Y";"N";"Y";"N";"N"]),[38;43;38;40;49],... [124 93;109 77; 125 83; 117 75; 122 80],... 'VariableNames',["Smoker" "Age" "BloodPressure"])

T=5×3 table Smoker Age BloodPressure ______ ___ _____________

  Y       38      124     93  
  N       43      109     77  
  Y       38      125     83  
  N       40      117     75  
  N       49      122     80  

Convert T to a scalar structure.

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

S = struct with fields: Smoker: [5x1 categorical] 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.

ans = 5×2

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(categorical(["Y";"N";"Y";"N";"N"]),[38;43;38;40;49],... [124 93;109 77; 125 83; 117 75; 122 80],... 'VariableNames',["Smoker" "Age" "BloodPressure"],... 'RowNames',["Chang" "Brown" "Ruiz" "Lee" "Smith"])

T=5×3 table Smoker Age BloodPressure ______ ___ _____________

Chang      Y       38      124     93  
Brown      N       43      109     77  
Ruiz       Y       38      125     83  
Lee        N       40      117     75  
Smith      N       49      122     80  

Convert T to a scalar structure.

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

S = struct with fields: Smoker: [5x1 categorical] Age: [5x1 double] BloodPressure: [5x2 double]

Add a field for the row names from the table.

S.RowNames = T.Properties.RowNames

S = struct with fields: Smoker: [5x1 categorical] 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

collapse all

T — Input table

table | timetable

Input table, specified as a table or timetable.

If T has variables whose names are not valid MATLAB® identifiers, then table2struct modifies them to create valid field names, primarily by removing spaces and replacing non-ASCII characters with underscores.

Extended Capabilities

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2013b