struct2table
Convert structure array to table
Syntax
T =struct2table(S)
T =struct2table(S,Name,Value)
Description
creates a table from a structure array,T
= struct2table(S
,Name,Value
)S
, with additional options specified by one or moreName,Value
pair arguments.
For example, you can specify row names to include in the table.
Examples
Convert Scalar Structure to Table
Convert a scalar structure to a table using the default options.
Create a structure array,S
.
S.Name = {'CLARK';'BROWN';'MARTIN'}; S.Gender = {'M';'F';'M'}; S.SystolicBP = [124;122;130]; S.DiastolicBP = [93;80;92]; S
S =struct with fields:Name: {3×1 cell} Gender: {3×1 cell} SystolicBP: [3×1 double] DiastolicBP: [3×1 double]
The scalar structure,S
, has four fields, each with three rows.
Convert the structure array to a table.
T =struct2table(S)
T =3×4 tableName Gender SystolicBP DiastolicBP ________ ______ __________ ___________ 'CLARK' 'M' 124 93 'BROWN' 'F' 122 80 'MARTIN' 'M' 130 92
The structure field names inS
become the variable names in the output table. The size ofT
is 3-by-4.
ChangeName
from a variable to row names by modifying the table property,T.Properties.RowNames
, and then deleting the variableName
.
T.Properties.RowNames = T.Name; T.Name = []; T
T =3×3 tableGender SystolicBP DiastolicBP ______ __________ ___________ CLARK 'M' 124 93 BROWN 'F' 122 80 MARTIN 'M' 130 92
Convert Nonscalar Structure Array to Table
Create a nonscalar structure array,S
.
S(1,1).Name ='CLARK'; S(1,1).Gender ='M'; S(1,1).SystolicBP = 124; S(1,1).DiastolicBP = 93; S(2,1).Name ='BROWN'; S(2,1).Gender ='F'; S(2,1).SystolicBP = 122; S(2,1).DiastolicBP = 80; S(3,1).Name ='MARTIN'; S(3,1).Gender ='M'; S(3,1).SystolicBP = 130; S(3,1).DiastolicBP = 92; S
S =3×1 struct array with fields:Name Gender SystolicBP DiastolicBP
S
is a 3-by-1 structure array with four fields.
Convert the structure array to a table.
T =struct2table(S)
T =3×4 tableName Gender SystolicBP DiastolicBP ________ ______ __________ ___________ 'CLARK' 'M' 124 93 'BROWN' 'F' 122 80 'MARTIN' 'M' 130 92
The structure field names inS
become the variable names in the output table. The size ofT
is 3-by-4.
Treat Scalar Stucture As Array
Use'AsArray',true
to create a table from a scalar structure whose fields have different numbers of rows.
Create a scalar structure,S
, with fieldsname
,billing
, andtest
.
S.name ='John Doe'; S.billing = 127.00; S.test = [79, 75, 73; 180, 178, 177.5; 220, 210, 205]; S
S =struct with fields:name: 'John Doe' billing: 127 test: [3×3 double]
The fields have a different number of rows. Therefore, you cannot usestruct2table(S)
, which uses'AsArray',false
by default.
Treat the scalar structure as an array and convert it to a table.
T =struct2table(S,'AsArray',真正的)
T =1×3 tablename billing test __________ _______ ____________ 'John Doe' 127 [3×3 double]
T
包含一个行。
Input Arguments
S
— Structure array
structure array
Structure array, specified as a scalar structure array.
If
S
is a scalar structure withn
fields, all of which havem
rows, thenT
is anm
-by-n
table.If
S
is a nonscalarm
-by-1
structure array withn
fields, thenT
is anm
-by-n
table.
Name-Value Pair Arguments
Specify optional comma-separated pairs ofName,Value
arguments.Name
is the argument name andValue
is the corresponding value.Name
must appear inside single quotes (' '
). You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN
.
RowNames',{'row1','row2','row3'}
uses the row names,row1
,row2
, androw3
for the table,T
.
'RowNames'
— Row names forT
{}
(default) | cell array of character vectors
Row names forT
, specified as the comma-separated pair consisting of'RowNames'
and a cell array of character vectors that are nonempty and distinct.
'AsArray'
— Indicator for how to treat scalar structure
false
(default) |true
|0
|1
Indicator for how to treat scalar structure, specified as the comma-separated pair consisting of'AsArray'
and eitherfalse
,true
,0
, or1
.
|
|
|
|
Output Arguments
T
— Output table
table
Output table, returned as a table. The table can store metadata such as descriptions, variable units, variable names, and row names. For more information, seeTable Properties.
Introduced in R2013b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- 意大利(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- 瑞士
- 联合王国(English)