Main Content

table2struct

Convert table to structure array

Description

example

S = table2struct(T)converts the table,T, to a structure array,S。每个变量Tbecomes a field inS。IfTis anm-by-ntable, thenSis am-by-1 structure array withnfields.

example

S = table2struct(T,'ToScalar',true)converts the table,T, to a scalar structureS。每个变量Tbecomes a field inS。IfTis am-by-ntable, thenShasnfields, each of which hasmrows.

Examples

collapse all

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

T = table(categorical({'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=5×3 tableGender Age BloodPressure ______ ___ _____________ M 38 124 93 M 43 109 77 F 38 125 83 F 40 117 75 F 49 122 80

ConvertTto a structure array.

S = table2struct(T)
S=5×1 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 ofScorrespond to the three variables fromT

Display the field data for the first element ofS

S(1)
ans =struct with fields:Gender: M Age: 38 BloodPressure: [124 93]

The information corresponds to the first row of the table.

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

T = table(categorical({'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=5×3 tableGender Age BloodPressure ______ ___ _____________ M 38 124 93 M 43 109 77 F 38 125 83 F 40 117 75 F 49 122 80

ConvertTto a scalar structure.

S = table2struct(T,'ToScalar',真正的)
S =struct with fields:Gender: [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 tableT

Display the data for the field血压

S.Blooppressure.
ans =5×2124 93 109 77 125 83 117 75 122 80

The structure field血压包含从表中相同名称的变量中的所有数据T

Create a table,T, that includes row names.

T = table(categorical({'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=5×3 tableGender 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

ConvertTto a scalar structure.

S = table2struct(T,'ToScalar',真正的)
S =struct with fields:Gender: [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:Gender: [5x1 categorical] Age: [5x1 double] BloodPressure: [5x2 double] RowNames: {5x1 cell}

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

Input Arguments

collapse all

Input table, specified as a table.

IfThas variables whose names are not valid MATLAB®identifiers, thentable2structmodifies them to create valid field names, primarily by removing spaces and replacing non-ASCII characters with underscores.

Extended Capabilities

Introduced in R2013b