Access Data in a Table

Ways to Index into a Table

A table is a container for storing column-oriented variables that have the same number of rows. Parentheses allow you to select a subset of the data in a table and preserve the table container. Curly braces and dot indexing allow you to extract data from a table.

If you use curly braces, the resulting array is the horizontal concatenation of the specified table variables containing only the specified rows. The data types of all the specified variables must be compatible for concatenation. You can then perform calculations using MATLAB® functions.

Dot indexing extracts data from one table variable. The result is an array of the same data type as extracted variable. You can follow the dot indexing with parentheses to specify a subset of rows to extract from a variable.

Summary of Table Indexing Methods

Consider a table, T.

Type of IndexingResultSyntaxrowsvars/var
ParenthesestableT(rows,vars)

One or more rows

One or more variables

Curly Bracesextracted dataT{rows,vars}

One or more rows

One or more variables

Dot Indexingextracted data

T.var

T.(varindex)

All rows

One variable

Dot Indexingextracted data

T.var(rows)

One or more rows

One variable

How to Specify Rows to Access

When indexing into a table with parentheses, curly braces, or dot indexing, you can specify rows as a colon, numeric indices, or logical expressions. Furthermore, you can index by name using a single row name or a cell array of row names.

A logical expression can contain curly braces or dot indexing to extract data from which you can define the subset of rows. For example, rows = T.Var2>0 returns a logical array with logical true (1) for rows where the value in the variable Var2 is greater than zero.

How to Specify Variables to Access

When indexing into a table with parentheses or curly braces, you can specify vars as a colon, numeric indices, logical expressions, a single variable name, or a cell array of row names.

When using dot indexing, you must specify a single variable to access. For a single variable name, use T.var. For a single variable index, specified as a positive integer, use T.(varindex).

Create Table from Subset of Larger Table

This example shows how to create a table from a subset of a larger table.

Load Sample Data

Load the sample patients data and create a table. Use the unique identifiers in LastName as row names.

load patients

patients = table(Age,Gender,Height,Weight,Smoker,...
    'RowNames',LastName);

The table, patients, contains 100 rows and 5 variables.

View the data type, description, units, and other descriptive statistics for each variable by using summary to summarize the table.

summary(patients)
Variables:

    Age: 100x1 double
        Values:

            min       25   
            median    39   
            max       50   

    Gender: 100x1 cell string

    Height: 100x1 double
        Values:

            min       60      
            median    67      
            max       72      

    Weight: 100x1 double
        Values:

            min         111   
            median    142.5   
            max         202   

    Smoker: 100x1 logical
        Values:

            true     34      
            false    66      

Index Using Numeric Indices

Create a subtable containing the first five rows and all the variables from the table, patients. Use numeric indexing within the parentheses to specify the desired rows and variables. This is similar to indexing with numeric arrays.

T1 = patients(1:5,:)
T1 = 

                Age     Gender     Height    Weight    Smoker
                ___    ________    ______    ______    ______

    Smith       38     'Male'      71        176       true  
    Johnson     43     'Male'      69        163       false 
    Williams    38     'Female'    64        131       false 
    Jones       40     'Female'    67        133       false 
    Brown       49     'Female'    64        119       false 

T1 is a 5-by-5 table. In addition to numeric indices, you can use row or variable names inside the parentheses. In this case, using row indices and a colon is more compact than using row or variable names.

Index Using Names

Select all the data for the patients with the last names 'Adams' and 'Brown'. In this case, it is simpler to use the row names than to use the numeric index.

T2 = patients({'Adams','Brown'},:)
T2 = 

             Age     Gender     Height    Weight    Smoker
             ___    ________    ______    ______    ______

    Adams    48     'Female'    66        137       false 
    Brown    49     'Female'    64        119       false 

T2 is a 2-by-5 table.

Index Using a Logical Expression

Create a new table, T3, containing the gender, height, and weight of the patients under the age of 30. Select only the rows where the value in the variable, Age, is less than 30.

Use dot notation to extract data from a table variable and a logical expression to define the subset of rows based on that extracted data.

rows = patients.Age<30;
vars = {'Gender','Height','Weight'};

rows is a 100-by-1 logical array containing logical true (1) for rows where the value in the variable, Age, is less than 30.

Use parentheses to return a table containing the desired subset of the data.

T3 = patients(rows,vars)
T3 = 

                  Gender     Height    Weight
                 ________    ______    ______

    Moore        'Male'      68        183   
    Jackson      'Male'      71        174   
    Garcia       'Female'    69        131   
    Walker       'Female'    65        123   
    Hall         'Male'      70        189   
    Young        'Female'    63        114   
    Hill         'Female'    64        138   
    Rivera       'Female'    63        130   
    Cooper       'Female'    65        127   
    Cox          'Female'    66        111   
    Howard       'Female'    68        134   
    James        'Male'      66        186   
    Jenkins      'Male'      69        189   
    Perry        'Female'    64        120   
    Alexander    'Male'      69        171   

T3 is a 15-by-3 table.

Create Array from the Contents of Table

This example shows how to extract the contents of a table using curly braces or dot indexing.

Load Sample Data

Load the sample patients data and create a table. Use the unique identifiers in LastName as row names.

load patients

patients = table(Age,Gender,Height,Weight,Smoker,...
    'RowNames',LastName);

The table, patients, contains 100 rows and 5 variables.

Extract Multiple Rows and Multiple Variables

Extract data from multiple variables in the table, patients by using curly braces. Since dot indexing extracts data from a single variable at a time, braces are more convenient when you want to extract more than one variable.

Extract the height and weight for the first five patients. Use numeric indices to select the subset of rows, 1:5, and variable names to select the subset of variables, {Height,Weight}.

A = patients{1:5,{'Height','Weight'}}
A =

    71   176
    69   163
    64   131
    67   133
    64   119

A is a 5-by-2 numeric array.

Extract Data from One Variable

Use dot indexing to easily extract the contents of a single variable. Plot a histogram of the numeric data in the variable, Weight.

figure()
hist(patients.Weight)
title(' Patient Weight')

patients.Weight is a double-precision column vector with 100 rows. Alternatively, you can use curly braces, patients{:,'Weight'}, to extract all the rows for the variable Weight.

To specify a subset of rows for a single variable, you can follow the dot indexing with parentheses or curly braces. Extract the heights of the nonsmoker patients under the age of 30.

Use dot notation to extract data from table variables and a logical expression to define the subset of rows based on that extracted data.

rows = patients.Smoker==false & patients.Age<30;

Use dot notation to extract the desired rows from the variable, Height.

patients.Height(rows)
ans =

    68
    71
    70
    63
    64
    63
    65
    66
    68
    66
    64

The output is a 11-by-1 numeric array. Alternatively, you can specify the single variable, Height, within curly braces to extract the desired data, patients{rows,'Height'}.

See Also

| |

Related Examples

More About

Was this topic helpful?