Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Table Properties

Access and modify table metadata 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 character vectors containing the specified information.

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

Table Metadata

expand all

Variable names, specified as a cell array of character vectors that are nonempty and distinct. Variable names must be valid MATLAB® variable names. The number of character vectors must equal the number of variables. MATLAB removes any leading or trailing white space from the character vectors.

If valid MATLAB identifiers are not available for use as variable names, MATLAB uses a cell array of N character vectors 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 =

  5×4 table

    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 using the Properties.VariableNames property.

T.Properties.VariableNames = {'Gender' 'Age' 'Height' 'Weight'}
T =

  5×4 table

    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. Include all rows, but subscript on Age and Gender.

T(:,{'Age','Gender'})
ans =

  5×2 table

    Age    Gender
    ___    ______

    38     'M'   
    43     'M'   
    38     'F'   
    40     'F'   
    49     'F'   

Extract data from T. Use variable names within curly braces to extract the data from the variables Height and Weight.

T{:,{'Height','Weight'}}
ans =

    71   176
    69   163
    64   131
    67   133
    64   119

Extract data from the variable Gender using dot indexing.

T.Gender
ans =

  5×1 cell array

    'M'
    'M'
    'F'
    'F'
    'F'

Extract all data using the Variables property. T.Variables is equivalent to T{:,:}. However, this syntax concatenates the table variables together. The data types of the table variables must be data types that can be concatenated.

Copy T to T2, and delete T2.Gender. The remaining variables of T2 are numeric. Concatenate them together into a numeric array using T2.Variables.

T2 = T;
T2.Gender = [];
T2.Variables
ans =

    38    71   176
    43    69   163
    38    64   131
    40    67   133
    49    64   119

Row names, specified as a cell array of character vectors that are nonempty and distinct. This property can be empty, but if not empty, the number of character vectors must equal the number of rows in the table. MATLAB removes any leading or trailing white space from the character vectors. 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 using the Properties.RowNames property. By default, tables do not have row names, but you can add them at any time.

T.Properties.RowNames = LastName;
head(T)
ans =

  8×7 table

                 Gender     Age    Height    Weight    Smoker    Systolic    Diastolic
                ________    ___    ______    ______    ______    ________    _________

    Smith       'Male'      38     71        176       true      124         93       
    Johnson     'Male'      43     69        163       false     109         77       
    Williams    'Female'    38     64        131       false     125         83       
    Jones       'Female'    40     67        133       false     117         75       
    Brown       'Female'    49     64        119       false     122         80       
    Davis       'Female'    46     68        142       false     121         70       
    Miller      'Female'    33     64        142       true      130         88       
    Wilson      'Male'      40     68        180       false     115         82       

Create a subtable. Include all variables, but subscript on a subset of row names.

T({'Smith','Williams'},:)
ans =

  2×7 table

                 Gender     Age    Height    Weight    Smoker    Systolic    Diastolic
                ________    ___    ______    ______    ______    ________    _________

    Smith       'Male'      38     71        176       true      124         93       
    Williams    'Female'    38     64        131       false     125         83       

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

Example

Create a table. Modify the names of its dimensions using the Properties.DimensionNames property.

load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic,...
          'RowNames',LastName);
T.Properties.DimensionNames = {'Patient' 'Data'};
T.Properties
ans = 

  struct with fields:

             Description: ''
                UserData: []
          DimensionNames: {'Patient'  'Data'}
           VariableNames: {1×7 cell}
    VariableDescriptions: {}
           VariableUnits: {}
                RowNames: {100×1 cell}

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

Example

Create a table. Modify the description of the table. Display a summary of the result.

load patients
T = table(Gender,Age,Height,Weight);
T.Properties.Description = 'Simulated patient data';
summary(T)
Description:  Simulated patient data

Variables:

    Gender: 100×1 cell array of character vectors

    Age: 100×1 double

        Values:

            Min       25   
            Median    39   
            Max       50   

    Height: 100×1 double

        Values:

            Min       60      
            Median    67      
            Max       72      

    Weight: 100×1 double

        Values:

            Min         111   
            Median    142.5   
            Max         202   

Variable descriptions, specified as a cell array of character vectors. This property can be an empty cell array, which is the default. If the cell array is not empty, the number of character vectors must equal the number of variables. You can specify an individual empty character vector 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. Modify the variable descriptions. Display a summary of the result.

load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
T.Properties.VariableDescriptions = {'' '' '' '' ...
                                     'Has the patient ever been a smoker' ...
                                     'Systolic Pressure' 'Diastolic Pressure'};
summary(T)
Variables:

    Gender: 100×1 cell array of character vectors

    Age: 100×1 double

        Values:

            Min       25   
            Median    39   
            Max       50   

    Height: 100×1 double

        Values:

            Min       60      
            Median    67      
            Max       72      

    Weight: 100×1 double

        Values:

            Min         111   
            Median    142.5   
            Max         202   

    Smoker: 100×1 logical

        Description:  Has the patient ever been a smoker
        Values:

            True     34      
            False    66      

    Systolic: 100×1 double

        Description:  Systolic Pressure
        Values:

            Min       109       
            Median    122       
            Max       138       

    Diastolic: 100×1 double

        Description:  Diastolic Pressure
        Values:

            Min         68       
            Median    81.5       
            Max         99       

Variable units, specified as a cell array of character vectors. This property can be an empty cell array, which is the default. If the cell array is not empty, the number of character vectors must equal the number of variables. You can specify an individual empty character vector 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. Modify the variable units. Display a summary of the result.

load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
T.Properties.VariableUnits = {'' 'Yrs' 'In' 'Lbs' '' 'mm Hg' 'mm Hg'};
summary(T)
Variables:

    Gender: 100×1 cell array of character vectors

    Age: 100×1 double

        Units:  Yrs
        Values:

            Min       25   
            Median    39   
            Max       50   

    Height: 100×1 double

        Units:  In
        Values:

            Min       60      
            Median    67      
            Max       72      

    Weight: 100×1 double

        Units:  Lbs
        Values:

            Min         111   
            Median    142.5   
            Max         202   

    Smoker: 100×1 logical

        Values:

            True     34      
            False    66      

    Systolic: 100×1 double

        Units:  mm Hg
        Values:

            Min       109       
            Median    122       
            Max       138       

    Diastolic: 100×1 double

        Units:  mm Hg
        Values:

            Min         68       
            Median    81.5       
            Max         99       

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

Example

Create a table. Attach an anonymous function as a piece of user data that is associated with the table.

load patients
T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
formula = @(x) x.^2;
T.Properties.UserData = formula;
T.Properties
ans = 

  struct with fields:

             Description: ''
                UserData: @(x)x.^2
          DimensionNames: {'Row'  'Variables'}
           VariableNames: {1×7 cell}
    VariableDescriptions: {}
           VariableUnits: {}
                RowNames: {}

Introduced in R2013b

Was this topic helpful?