Main Content

table2struct

テーブルの構造体配列への変換

説明

S = table2struct(T) は、table または timetable T を構造体配列 S に変換します。T の各変数は S の 1 つのフィールドになります。Tmn 列の table または timetable である場合、Sn 個のフィールドをもつ m 行 1 列の構造体配列です。

出力 S は、T.Properties のテーブル プロパティを含みません。

  • T が行名をもつ table である場合、S は行名を含みません。

  • T が timetable の場合、S は行時間を含みません。

S = table2struct(T,"ToScalar",true) は、table T をスカラー構造体 S に変換します。T の各変数は S の 1 つのフィールドになります。Tmn 列のテーブルの場合、Sn 個のフィールドをもち、各フィールドが m 行になります。

すべて折りたたむ

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 の行名をもつフィールドを含めます。

入力引数

すべて折りたたむ

入力 table。table または timetable として指定します。

T に含まれる変数の名前が有効な MATLAB® 識別子でない場合、table2struct は、主にスペースを削除したり、非 ASCII 文字をアンダースコアに置き換えたりすることで名前を変更して、有効なフィールド名を作成します。

拡張機能

スレッドベースの環境
MATLAB® の backgroundPool を使用してバックグラウンドでコードを実行するか、Parallel Computing Toolbox™ の ThreadPool を使用してコードを高速化します。

バージョン履歴

R2013b で導入