Create a Table

This example shows how to create a table. table is a data type to collect heterogeneous data and metadata properties, such as variable names, row names, descriptions, and variable units, in a single container.

Tables are suitable for column-oriented or tabular data that are often stored as columns in a text file or in a spreadsheet. Each column in a file becomes a variable in the table. Each variable (column) in a table can have a different data type and a different size, but each variable must have the same number of rows. For example, you can use a table to store experimental data, with rows representing different observations and columns representing different measured variables.

View Sample Data from a File

The sample comma-delimited text file, patients.dat, contains information on 100 different patients. You can use type patients.dat to view the contents of the file. Below are the first six lines.

LastName,Gender,Age,Location,Height,Weight,Smoker,Systolic,Diastolic,SelfAssessedHealthStatus
Smith,Male,38,County General Hospital,71,176,1,124,93,Excellent
Johnson,Male,43,VA Hospital,69,163,0,109,77,Fair
Williams,Female,38,St. Mary's Medical Center,64,131,0,125,83,Good
Jones,Female,40,VA Hospital,67,133,0,117,75,Fair
Brown,Female,49,County General Hospital,64,119,0,122,80,Good

The first line contains column headings and each tabular entry is separated by a comma.

Create a Table Using Default Settings

Convert the sample delimited text file, patients.dat, to a table using the readtable function with default settings. You also can use the readtable function to convert an Excel® spreadsheet file to a table. The readtable function accepts delimited text files with extensions .txt, .dat, or .csv and spreadsheet files with extensions .xls or .xlsx.

T = readtable('patients.dat');

Return the size of the table.

size(T)
ans =

   100    10

The table, T, contains 100 rows and ten variables.

Display the first five rows and first five variables of the table.

T(1:5,1:5)
ans = 

     LastName      Gender     Age             Location              Height
    __________    ________    ___    ___________________________    ______

    'Smith'       'Male'      38     'County General Hospital'      71    
    'Johnson'     'Male'      43     'VA Hospital'                  69    
    'Williams'    'Female'    38     'St. Mary's Medical Center'    64    
    'Jones'       'Female'    40     'VA Hospital'                  67    
    'Brown'       'Female'    49     'County General Hospital'      64    

By default, readtable uses the first row of the text file for variable names. If the first row does not contain variable names, you can specify the optional name-value pair argument T = readtable(filename,'ReadVariableNames',false to change the default behavior.

Additionally, you can create a table by combining or converting variables that exist in the MATLAB® workspace. This table summarizes the functions you can use to create a table from workspace variables.

Data Source in MATLAB WorkspaceFunction for Conversion to Table
Heterogeneous collection of workspace variablestable
Numeric arrayarray2table
Cell arraycell2table
Structure arraystruct2table

Summarize the Table

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

format compact

summary(T)
Variables:
    LastName: 100x1 cell string
    Gender: 100x1 cell string
    Age: 100x1 double
        Values:
            min       25   
            median    39   
            max       50   
    Location: 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 double
        Values:
            min       0       
            median    0       
            max       1       
    Systolic: 100x1 double
        Values:
            min       109       
            median    122       
            max       138       
    Diastolic: 100x1 double
        Values:
            min         68       
            median    81.5       
            max         99       
    SelfAssessedHealthStatus: 100x1 cell string

The table is heterogeneous. The variables LastName, Gender, Location, and SelfAssessedHealthStatus are cell arrays of strings, and the other variables are numeric with a data type of double.

Set the format back to the default.

format

Add Row Names

Tables can be subscripted using parentheses much like ordinary numeric arrays. But in addition to numeric and logical indices, you can use variable and row names as indices. Use the unique identifiers in the variable, LastName, as row names. Then, delete the variable LastName from the table.

T.Properties.RowNames = T.LastName;
T.LastName = [];

Alternatively, you can import the file using readtable with the 'ReadRowNames',true name-value pair argument to indicate that the first column contains row names.

Return the size of the table.

size(T)
ans =

   100     9

T contains 100 rows and nine variables. The row and variable names, respectively, are not included in the size of a table.

Display the first five rows and last four variables of the table.

T(1:5,6:9)
ans = 

                Smoker    Systolic    Diastolic    SelfAssessedHealthStatus
                ______    ________    _________    ________________________

    Smith       1         124         93           'Excellent'             
    Johnson     0         109         77           'Fair'                  
    Williams    0         125         83           'Good'                  
    Jones       0         117         75           'Fair'                  
    Brown       0         122         80           'Good'                  

The elements of the first column in the text file, LastName, are now row names. Row names and variable names are table properties. You can always add or change the row names of an existing table by modifying the property T.Properties.RowNames.

Combine Two Variables into a Single Variable

Create a new variable for blood pressure as a horizontal concatenation of the two variables Systolic and Diastolic. Then, delete the variables Systolic and Diastolic.

T.BloodPressure = [T.Systolic T.Diastolic];
T(:,{'Systolic' 'Diastolic'}) = [];

Return the size of the table.

size(T)
ans =

   100     8

T contains 100 rows and eight variables. The row and variable names, respectively, are not included in the size of a table. Furthermore, size counts eight variables, even though the variable BloodPressure contains two columns.

Display the first five rows and last four variables of the table.

T(1:5,5:8)
ans = 

                Weight    Smoker    SelfAssessedHealthStatus     BloodPressure 
                ______    ______    ________________________    _______________

    Smith       176       1         'Excellent'                 124          93
    Johnson     163       0         'Fair'                      109          77
    Williams    131       0         'Good'                      125          83
    Jones       133       0         'Fair'                      117          75
    Brown       119       0         'Good'                      122          80

The new variable, BloodPressure contains two columns of data and is the last variable in the table.

Create a New Variable from Existing Variables

Create a new variable in the table, BMI, based on data in existing variables, Weight and Height. Then populate the variable units and variable descriptions properties for this variable.

T.BMI = (T.Weight*0.453592)./(T.Height*0.0254).^2;
T.Properties.VariableUnits{'BMI'} = 'kg/m^2';
T.Properties.VariableDescriptions{'BMI'} = 'Body Mass Index';

Return the size of the table.

size(T)
ans =

   100     9

T contains 100 rows and nine variables.

Display the first five rows and last four variables of the table.

T(1:5,6:9)
ans = 

                Smoker    SelfAssessedHealthStatus     BloodPressure      BMI  
                ______    ________________________    _______________    ______

    Smith       1         'Excellent'                 124          93    24.547
    Johnson     0         'Fair'                      109          77    24.071
    Williams    0         'Good'                      125          83    22.486
    Jones       0         'Fair'                      117          75    20.831
    Brown       0         'Good'                      122          80    20.426

See Also

| | | | | |

Related Examples

More About

Was this topic helpful?