table

Methods

Method Reference: table.addprop

table: T = addprop (T, propertyNames, propertyTypes)

Add custom properties to a table.

T = addprop (T, propertyNames, propertyTypes) adds properties that contain custom metadata to the table T. The input argument propertyNames specifies the names of the custom properties to be added and propertyTypes the type of each corresponding custom property, that is whether the metadata values contained in the property apply to table T as a whole, or to the variables of T. Both propertyNames and propertyTypes can be character vectors, cell arrays of character vectors, or strings. When defined as cell arrays of character vectors or strings, they must have the same number of elements.

Valid propertyTypes are either 'table' or 'variable'. When defined as 'table', the custom property can contain a scalar value of arbitrary type, which applies as metadata to the table as a whole. When defined as 'variable', the custom property contains a vector with one element per variable in the table.

After adding custom properties using addprop, metadata values can be assigned to the properties using dot syntax.

Source Code: table

Example: 1

addprop attaches your own metadata to a table. Each property is declared with a scope: a 'table'-scoped property holds a single value for the whole table, while a 'variable'-scoped property holds one value per variable, aligned with the table's columns.

 Age = [38; 43; 40];
 Height = duration (0, 0, 0) + hours ([1.9; 1.8; 1.7]);
 Gender = categorical ({'M'; 'F'; 'M'});
 T = table (Age, Height, Gender)
T =
  3x3 table

    Age     Height     Gender    
    ___    ________    ______    

     38    01:54:00         M    
     43    01:48:00         F    
     40    01:42:00         M
 T = addprop (T, {'DataSource', 'Instrument'}, {'table', 'variable'});

The table-scoped DataSource takes one value; the variable-scoped Instrument takes a cell with one entry per variable (Age, Height, Gender).

 T.Properties.CustomProperties.DataSource = 'Clinic A';
 T.Properties.CustomProperties.Instrument = {'form', 'stadiometer', 'form'};
 T.Properties.CustomProperties
ans =

  scalar structure containing the fields:

    DataSource = Clinic A
    Instrument =
    {
      [1,1] = form
      [1,2] = stadiometer
      [1,3] = form
    }

Because it is variable-scoped, Instrument follows column operations — after removing Height its entry drops out too, staying aligned with the columns.

 T2 = removevars (T, 'Height');
 T2.Properties.CustomProperties.Instrument
ans =
  1x2 cell array

    {'form'}    {'form'}