Main Content

R2016b における DimensionNames プロパティへの変更

table データ型は、単一コンテナー内の列方向の異種混合データの収集に適しています。table は、変数名、行名、次元名、説明、変数の単位などのメタデータ プロパティも格納します。R2016b 以降、次元名を使用して、ドット表記の添字により table データおよびメタデータにアクセスできます。これをサポートするには、次元名が変数名と同じ要件を満たさなければなりません。下位互換性のために、table では必要に応じて次元名を自動的に変更することによってこれらの制限を適用します。

行名と変数名をもつ table を作成します。

Number = [8; 21; 13; 20; 11];
Name = {'Van Buren'; 'Arthur'; 'Fillmore'; 'Garfield'; 'Polk'};
Party = categorical({'Democratic'; 'Republican'; 'Whig'; 'Republican'; 'Republican'});
T = table(Number,Party,'RowNames',Name)
T = 

                 Number      Party   
                 ______    __________

    Van Buren     8        Democratic
    Arthur       21        Republican
    Fillmore     13        Whig      
    Garfield     20        Republican
    Polk         11        Republican

次元名を含め、そのプロパティを表示します。次元名の既定値は 'Row''Variables' です。

T.Properties
ans = 

  struct with fields:

             Description: ''
                UserData: []
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {'Number'  'Party'}
    VariableDescriptions: {}
           VariableUnits: {}
                RowNames: {5×1 cell}

R2016b 以降、新しい名前を次元名に割り当て、それを使用して table データにアクセスできます。次元名は有効な MATLAB® 識別子でなければならず、予約名の 'Properties''RowNames' または 'VariableNames' であってはなりません。

新しい名前を最初の次元名に割り当て、それを使用して table の行名にアクセスします。

T.Properties.DimensionNames{1} = 'Name';
T.Name
ans =

  5×1 cell array

    'Van Buren'
    'Arthur'
    'Fillmore'
    'Garfield'
    'Polk'

Name という新しい table 変数を作成します。この変数を作成すると、競合を避けるために table で最初の次元名が変更されます。更新後の次元名は Name_1 になります。

T{:,'Name'} = {'Martin'; 'Chester'; 'Millard'; 'James'; 'James'}
Warning: DimensionNames property was modified to avoid conflicting dimension and variable names: 
 'Name'. See Compatibility Considerations for Using Tables for more details. This will become an  
error in a future release. 

T = 

                 Number      Party         Name   
                 ______    __________    _________

    Van Buren     8        Democratic    'Martin' 
    Arthur       21        Republican    'Chester'
    Fillmore     13        Whig          'Millard'
    Garfield     20        Republican    'James'  
    Polk         11        Republican    'James'  
T.Properties.DimensionNames
ans =

  1×2 cell array

    'Name_1'    'Data'

同様に、有効な MATLAB 識別子ではない次元名を割り当てた場合にも、名前が変更されます。

T.Properties.DimensionNames{1} = 'Last Name';
T.Properties.DimensionNames
Warning: DimensionNames property was modified to make the name 'Last Name' a valid MATLAB
identifier. See Compatibility Considerations for Using Tables for more details. This will  
become an error in a future release. 

ans =

  1×2 cell array

    'LastName'    'Data'

R2016b では、次元名が有効な識別子でなかったり、変数名や予約名と競合する場合、以前のリリースで作成されたコードや table の作業を続行できるよう、警告が表示されます。このような警告が表示された場合、コードを更新して警告を回避することを推奨します。