Documentation Center

  • Trial Software
  • Product Updates

Table Properties

Access and modify table metadata properties

Access and Modify Properties

A table, T, has properties that store metadata such as its variable names, row names, descriptions, and variable units. T.Properties returns a summary of all of the table properties.

You can access a property using T.Properties.PropName, where T is the name of the table and PropName is one of the table properties. For example, to access the VariableDescriptions property of a table named Patients, use Patients.Properties.VariableDescriptions.

You can modify a property value using T.Properties.PropName = P where T is the name of the table, PropName is one of the table properties, and P is the desired property value. For example, to modify the VariableUnits property of a table named Patients, use Patients.Properties.VariableUnits = P where P is a cell array of strings containing the specified information.

In contrast, you can access and modify variables within a table using T.Variable or T.Variable = V, where T is the name of the table, Variable is the name of the variable you want to access or modify, and V is the variable value you want.

Properties

expand all

VariableNames — Variable namescell array of nonempty, distinct strings

Variable names, specified as a cell array of nonempty, distinct strings. The number of strings must equal the number of variables. MATLAB® removes any leading or trailing white space from the strings.

If valid MATLAB identifiers are not available for use as variable names, MATLAB uses a cell array of N strings of the form {'Var1' ... 'VarN'} where N is the number of variables. You can determine valid MATLAB variable names using the function isvarname.

The variable names are visible when viewing the table and when using the summary function. Furthermore, you can use the variable names within parentheses, within curly braces, or with dot indexing to access table data.

Example

%% Create a table
T = table(['M';'M';'F';'F';'F'],[38;43;38;40;49],...
    [71;69;64;67;64],[176;163;131;133;119])
T = 

    Var1    Var2    Var3    Var4
    ____    ____    ____    ____

    M       38      71      176 
    M       43      69      163 
    F       38      64      131 
    F       40      67      133 
    F       49      64      119 
%% Modify variable names
T.Properties.VariableNames = {'Gender' 'Age' 'Height' 'Weight'}
T = 

    Gender    Age    Height    Weight
    ______    ___    ______    ______

    M         38     71        176   
    M         43     69        163   
    F         38     64        131   
    F         40     67        133   
    F         49     64        119   
%% Create a subtable
%
% Use variable names within parentheses to create a subtable.
% Include al the rows, but only the variables Age and Gender.
T(:,{'Age','Gender'})
ans = 

    Age    Gender
    ___    ______

    38     M     
    43     M     
    38     F     
    40     F     
    49     F     
%% Extract data from the table
%
% Use variable names within curly braces to extract the 
% numeric data from the variables Height and Weight.
T{:,{'Height','Weight'}}
ans =

    71   176
    69   163
    64   131
    67   133
    64   119
% Use variable names with dot indexing to extract 
% all the data from the variable Gender.
T.Gender
ans =

M
M
F
F
F

RowNames — Row names{} (default) | cell array of nonempty, distinct strings

Row names, specified as a cell array of nonempty, distinct strings. This property can be empty, but if not empty, the number of strings must equal the number of rows in the table. MATLAB removes any leading or trailing white space from the strings. The default property value is an empty cell array.

The row names are visible when you view the table. Furthermore, you can use the row names within parentheses or curly braces to access the table data.

Example

% Create a table
load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);

% Add row names
T.Properties.RowNames = LastName;

% Remove row names
T.Properties.RowNames = {};

DimensionNames — Dimension names{'Row' ‘Variable'} (default) | two-element cell array of strings

Dimension names, specified as a two-element cell array of strings.

Example

% Create a table
load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic,...
    'RowNames',LastName);

% Add dimension names
T.Properties.DimensionNames = {'Patient' 'Data'};

Description — Table description'' (default) | string

Table description, specified as a string. This string is visible when using the summary function.

Example

% Create a table
load patients
T = table(Gender,Age,Height,Weight);

% Add a table description
T.Properties.Description = 'Simulated patient data';

%View the summary
format compact
summary(T)
Description:  Simulated patient data
Variables:
    Gender: 100x1 char
    Age: 100x1 double
        Values:
            min       25   
            median    39   
            max       50   
    Height: 100x1 double
        Values:
            min       60      
            median    67      
            max       72      
    Weight: 100x1 double
        Values:
            min         111   
            median    142.5   
            max         202   

VariableDescriptions — Variable descriptions{} (default) | cell array of strings

Variable descriptions, specified as a cell array of strings. This property can be an empty cell array, which is the default. If the cell array is not empty, the number of strings must equal the number of variables. You can specify an individual empty string within the cell array for a variable that does not have a description.

The variable descriptions are visible when using the summary function.

Example

% Create a table
load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);

% Add variable descriptions
T.Properties.VariableDescriptions = {'' '' '' '' ...
    'Has the patient ever been a smoker' ...
    'Systolic Pressure' 'Diastolic Pressure'};

% View the summary
format compact
summary(T)
Variables:
    Gender: 100x1 char
    Age: 100x1 double
        Values:
            min       25   
            median    39   
            max       50   
    Height: 100x1 double
        Values:
            min       60      
            median    67      
            max       72      
    Weight: 100x1 double
        Values:
            min         111   
            median    142.5   
            max         202   
    Smoker: 100x1 logical
        Description:  Has the patient ever been a smoker
        Values:
            true     34      
            false    66      
    Systolic: 100x1 double
        Description:  Systolic Pressure
        Values:
            min       109       
            median    122       
            max       138       
    Diastolic: 100x1 double
        Description:  Diastolic Pressure
        Values:
            min         68       
            median    81.5       
            max         99       
% Remove all the variable descriptions
T.Properties.VariableDescriptions = {};

VariableUnits — Variable units{} (default) | cell array of strings

Variable units, specified as a cell array of strings. This property can be an empty cell array, which is the default. If the cell array is not empty, the number of strings must equal the number of variables. You can specify an individual empty string within the cell array for a variable that does not have units.

The variable units are visible when using the summary function.

Example

% Create a table
load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);

% Add variable units
T.Properties.VariableUnits = {'' 'Yrs' 'In' 'Lbs' '' 'mm Hg' 'mm Hg'};

% View the summary
format compact
summary(T)
Variables:
    Gender: 100x1 char
    Age: 100x1 double
        Units:  Yrs
        Values:
            min       25   
            median    39   
            max       50   
    Height: 100x1 double
        Units:  In
        Values:
            min       60      
            median    67      
            max       72      
    Weight: 100x1 double
        Units:  Lbs
        Values:
            min         111   
            median    142.5   
            max         202   
    Smoker: 100x1 logical
        Values:
            true     34      
            false    66      
    Systolic: 100x1 double
        Units:  mm Hg
        Values:
            min       109       
            median    122       
            max       138       
    Diastolic: 100x1 double
        Units:  mm Hg
        Values:
            min         68       
            median    81.5       
            max         99       
% Remove all the variable units
T.Properties.VariableUnits = {};

UserData — Additional table information{} (default) | variable containing information in any data type

Additional table information, specified as a variable containing information in any data type.

Example

% Create a table
load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);

% Add an anonymous function to the table metadata
formula = @(x) x.^2;
T.Properties.UserData = formula;

See Also

| |

Was this topic helpful?