table2struct
テーブルの構造体配列への変換
説明
S = table2struct(
は、table または timetable T
)T
を構造体配列 S
に変換します。T
の各変数は S
の 1 つのフィールドになります。T
が m
行 n
列の table または timetable である場合、S
は n
個のフィールドをもつ m
行 1 列の構造体配列です。
出力 S
は、T.Properties
のテーブル プロパティを含みません。
T
が行名をもつ table である場合、S
は行名を含みません。T
が timetable の場合、S
は行時間を含みません。
例
テーブルの構造体配列への変換
5 つの行と 3 つの変数をもつ table 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 列で、table T
の 5 つの行に対応します。S
の 3 つのフィールドは、T
の 3 つの変数に対応します。
S
の最初の要素のフィールド データを表示します。
S(1)
ans = struct with fields:
Smoker: Y
Age: 38
BloodPressure: [124 93]
この情報はテーブルの最初の行に対応します。
テーブルのスカラー構造体への変換
5 つの行と 3 つの変数をもつ table 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 列で、table T
の 5 つの行に対応します。
BloodPressure
フィールドのデータを表示します。
S.BloodPressure
ans = 5×2
124 93
109 77
125 83
117 75
122 80
構造体フィールド BloodPressure
には、table T
の同じ名前の変数にあったデータがすべて含まれます。
行名を含むテーブルの構造体への変換
行名を含む table 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{:}
を使用して、table の行名をもつフィールドを含めます。
入力引数
T
— 入力テーブル
table | timetable
入力 table。table または timetable として指定します。
T
に含まれる変数の名前が有効な MATLAB® 識別子でない場合、table2struct
は、主にスペースを削除したり、非 ASCII 文字をアンダースコアに置き換えたりすることで名前を変更して、有効なフィールド名を作成します。
拡張機能
C/C++ コード生成
MATLAB® Coder™ を使用して C および C++ コードを生成します。
使用上の注意事項および制限事項:
生成コードでこの関数を使用する場合、入力 table は一定でなければなりません。詳細については、table のコード生成 (MATLAB Coder)とコード生成における table の制限事項 (MATLAB Coder)を参照してください。
バージョン履歴
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)