Matlab中table2struct函数使用

目录

语法

说明

示例

将表转换为结构体数组

将表转换为标量结构体

将带有行名称的表转换为结构体


        table2struct函数的功能是将表转换为结构体数组。

语法

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

        输入表,指定为表。如果 T 中的变量的名称不是有效的 MATLAB® 标识符,则table2struct会修改变量名以创建有效的字段名称,主要方法是删除空格并用下划线替换非 ASCII 字符。

说明

S = table2struct(T) 将表 T 转换为结构体数组 S。T 的每个变量都会成为 S 中的字段。如果 T 为 m×n 表,则 S 为包含 n 个字段的 mx1 结构体数组。

S = table2struct(T,'ToScalar',true) 将表 T 转换为标量结构体 S。T 的每个变量都会成为 S 中的字段。如果 T 为 m×n 表,则 S 包含 n 个字段,其中每个字段的行数都为 m。

示例

将表转换为结构体数组

        创建一个包含五行和三个变量的表 T

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 table
    Gender    Age    BloodPressure
    ______    ___    _____________

      M       38      124     93  
      M       43      109     77  
      F       38      125     83  
      F       40      117     75  
      F       49      122     80  

        将 T 转换为结构体数组。

S = table2struct(T)

S=5×1 struct array with fields:
    Gender
    Age
    BloodPressure

        该结构体为 5×1,与表 T 中的五行对应。S 的三个字段与 T 中的三个变量对应。显示 S 的第一个元素的字段数据。

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

        该信息与表中的第一行对应。

将表转换为标量结构体

        创建一个包含五行和三个变量的表 T

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 table
    Gender    Age    BloodPressure
    ______    ___    _____________

      M       38      124     93  
      M       43      109     77  
      F       38      125     83  
      F       40      117     75  
      F       49      122     80  

        将 T 转换为标量结构体。

S = table2struct(T,'ToScalar',true)
S = struct with fields:
           Gender: [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({'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 table
                Gender    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  

        将 T 转换为标量结构体。

S = table2struct(T,'ToScalar',true)
S = struct with fields:
           Gender: [5x1 categorical]
              Age: [5x1 double]
    BloodPressure: [5x2 double]

        为表中的行名称添加字段。

S.RowNames = T.Properties.RowNames
S = struct with fields:
           Gender: [5x1 categorical]
              Age: [5x1 double]
    BloodPressure: [5x2 double]
         RowNames: {5x1 cell}

        如果 S 为非标量结构体,使用 [S.RowNames] = T.Properties.RowNames{:} 包含行名称来自表中的字段。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值