table2struct
将表转换为结构体数组
说明
示例
将表转换为结构体数组
创建一个包含五行和三个变量的表 T
。
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
将 T
转换为结构体数组。
S = table2struct(T)
S=5×1 struct array with fields:
Smoker
Age
BloodPressure
该结构体为 5×1,与表 T
中的五行对应。S
的三个字段与 T
中的三个变量对应。
显示 S
的第一个元素的字段数据。
S(1)
ans = struct with fields:
Smoker: Y
Age: 38
BloodPressure: [124 93]
该信息与表中的第一行对应。
将表转换为标量结构体
创建一个包含五行和三个变量的表 T
。
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
将 T
转换为标量结构体。
S = table2struct(T,"ToScalar",true)
S = struct with fields:
Smoker: [5x1 categorical]
Age: [5x1 double]
BloodPressure: [5x2 double]
标量结构体字段中的数据为 5×1,与表 T
中的五行对应。
显示字段 BloodPressure
的数据。
S.BloodPressure
ans = 5×2
124 93
109 77
125 83
117 75
122 80
结构体字段 BloodPressure
包含表 T
中的同名变量的所有数据。
将带有行名称的表转换为结构体
创建一个包含行名称的表 T
。
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
将 T
转换为标量结构体。
S = table2struct(T,"ToScalar",true)
S = struct with fields:
Smoker: [5x1 categorical]
Age: [5x1 double]
BloodPressure: [5x2 double]
为表中的行名称添加字段。
S.RowNames = T.Properties.RowNames
S = struct with fields:
Smoker: [5x1 categorical]
Age: [5x1 double]
BloodPressure: [5x2 double]
RowNames: {5x1 cell}
如果 S
为非标量结构体,请使用 [S.RowNames] = T.Properties.RowNames{:}
包含行名称来自表中的字段。
输入参数
T
— 输入表
表 | 时间表
输入表,指定为表或时间表。
如果 T
中的变量的名称不是有效的 MATLAB® 标识符,则 table2struct
会修改变量名以创建有效的字段名称,主要方法是删除空格并用下划线替换非 ASCII 字符。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
在生成的代码中,使用该函数时,输入表必须为常量。有关详细信息,请参阅 表的代码生成 (MATLAB Coder) 和 Table Limitations for Code Generation (MATLAB Coder)。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
版本历史记录
在 R2013b 中推出
另请参阅
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)