# table2struct

## 语法

``S = table2struct(T)``
``S = table2struct(T,"ToScalar",true)``

## 说明

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

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

## 示例

```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 ```

`S(1)`
```ans = struct with fields: Smoker: Y Age: 38 BloodPressure: [124 93] ```

```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] ```

`S.BloodPressure`
```ans = 5×2 124 93 109 77 125 83 117 75 122 80 ```

```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} ```