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 any value of any type and size, which applies as metadata to
the table as a whole and is stored exactly as it is given. When defined
as 'variable', the custom property contains a vector with one
element per variable in the table.
A 'variable' property is cleared by assigning an empty 0-by-0
value, such as [] or {}, whatever the width of the
table; empty values of any other size are not accepted. A character
vector is not a valid value for a 'variable' property: use a
cell array of character vectors or a string array instead.
After adding custom properties using addprop, metadata values can
be assigned to the properties using dot syntax.
Source Code: table
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 =
CustomProperties with properties:
DataSource: 'Clinic A'
Instrument: {'form' 'stadiometer' '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'}