MATLAB 帮助中心
将表转换为结构体数组
S = table2struct(T)
S = table2struct(T,"ToScalar",true)
S = table2struct(T) 将表或时间表 T 转换为结构体数组 S。T 中的每个变量都成为 S 中的一个字段。如果 T 为 m×n 表或时间表,则 S 为包含 n 个字段的 m×1 结构体数组。
S
T
m
n
输出 S 不包括 T.Properties 中的表属性。
T.Properties
如果 T 是包含行名称的表,则 S 不包含行名称。
如果 T 是时间表,则 S 不包括行时间。
示例
S = table2struct(T,"ToScalar",true) 将表 T 转换为标量结构体 S。T 的每个变量都会成为 S 中的字段。如果 T 为 m×n 表,则 S 包含 n 个字段,其中每个字段的行数都为 m。
全部折叠
创建一个包含五行和三个变量的表 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=5×1 struct array with fields: Smoker Age BloodPressure
显示 S 的第一个元素的字段数据。
S(1)
ans = struct with fields: Smoker: Y Age: 38 BloodPressure: [124 93]
该信息与表中的第一行对应。
将 T 转换为标量结构体。
S = table2struct(T,ToScalar=true)
S = struct with fields: Smoker: [5×1 categorical] Age: [5×1 double] BloodPressure: [5×2 double]
标量结构体字段中的数据数组全部包含五行,与表 T 中的五行对应。
显示字段 BloodPressure 的数据。
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
为表中的行名称添加字段。
S.RowNames = T.Properties.RowNames
S = struct with fields: Smoker: [5×1 categorical] Age: [5×1 double] BloodPressure: [5×2 double] RowNames: {5×1 cell}
如果 S 为非标量结构体,请使用 [S.RowNames] = T.Properties.RowNames{:} 包含行名称来自表中的字段。
[S.RowNames] = T.Properties.RowNames{:}
输入表,指定为表或时间表。
如果 T 中的变量的名称不是有效的 MATLAB® 标识符,则 table2struct 会修改变量名以创建有效的字段名称,主要方法是删除空格并用下划线替换非 ASCII 字符。
table2struct
输出结构体数组。
全部展开
用法说明和限制:
在生成的代码中,使用该函数时,输入表必须为常量。有关详细信息,请参阅 表的代码生成 (MATLAB Coder) 和 代码生成的表限制 (MATLAB Coder)。
请参阅“C/C++ 代码生成”部分中的用法说明和限制。同样的用法说明和限制也适用于 GPU 代码生成。
backgroundPool
ThreadPool
在 R2013b 中推出
struct2table | table2cell | table2array | table
struct2table
table2cell
table2array
table
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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处