Add and Delete Table Variables

This example shows how to add and delete column-oriented variables in a table. You also can edit tables using the Variables Editor.

Load Sample Data

Load the sample patients data and create two tables. Create one table, T, with information collected from a patient questionnaire and create another table, T1, with data measured from the patient.

load patients

T = table(Age,Gender,Smoker);
T1 = table(Height,Weight,Systolic,Diastolic);

Display the first five rows of each table.

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

    Age     Gender     Smoker
    ___    ________    ______

    38     'Male'      true  
    43     'Male'      false 
    38     'Female'    false 
    40     'Female'    false 
    49     'Female'    false 


ans = 

    Height    Weight    Systolic    Diastolic
    ______    ______    ________    _________

    71        176       124         93       
    69        163       109         77       
    64        131       125         83       
    67        133       117         75       
    64        119       122         80       

The table T has 100 rows and 3 variables.

The table T1 has 100 rows and 4 variables.

Add Variables by Concatenating Tables

Add variables to the table, T, by horizontally concatenating it with T1.

T = [T T1];

Display the first five rows of the table, T.

T(1:5,:)
ans = 

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

    38     'Male'      true      71        176       124         93       
    43     'Male'      false     69        163       109         77       
    38     'Female'    false     64        131       125         83       
    40     'Female'    false     67        133       117         75       
    49     'Female'    false     64        119       122         80       

The table, T, now has 7 variables and 100 rows.

If the tables that you are horizontally concatenating have row names, horzcat concatenates the tables by matching the row names. Therefore, the tables must use the same row names, but the row order does not matter.

Add and Delete Variables by Name

First 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 by name using dot indexing.

T.BloodPressure = [T.Systolic T.Diastolic];

T.Systolic = [];
T.Diastolic = [];

Alternatively, you can also use parentheses with named indexing to delete the variables Systolic and Diastolic at once, T(:,{'Systolic','Diastolic'}) = [];.

Display the first five rows of the table, T.

T(1:5,:)
ans = 

    Age     Gender     Smoker    Height    Weight    BloodPressure
    ___    ________    ______    ______    ______    _____________

    38     'Male'      true      71        176       124     93   
    43     'Male'      false     69        163       109     77   
    38     'Female'    false     64        131       125     83   
    40     'Female'    false     67        133       117     75   
    49     'Female'    false     64        119       122     80   

T now has 6 variables and 100 rows.

Add a new variable, BMI, in the table, T, to contain the body mass index for each patient. BMI is a function of height and weight.

T.BMI = (T.Weight*0.453592)./(T.Height*0.0254).^2;

The operators ./ and .^ in the calculation of BMI indicate element-wise division and exponentiation, respectively.

Display the first five rows of the table, T.

T(1:5,:)
ans = 

    Age     Gender     Smoker    Height    Weight    BloodPressure     BMI  
    ___    ________    ______    ______    ______    _____________    ______

    38     'Male'      true      71        176       124     93       24.547
    43     'Male'      false     69        163       109     77       24.071
    38     'Female'    false     64        131       125     83       22.486
    40     'Female'    false     67        133       117     75       20.831
    49     'Female'    false     64        119       122     80       20.426

T has 100 rows and 7 variables.

Delete Variables by Number

Delete the third variable, Smoker, and the sixth variable, BloodPressure, from the table.

T(:,[3,6]) = [];

Display the first five rows of the table, T.

T(1:5,:)
ans = 

    Age     Gender     Height    Weight     BMI  
    ___    ________    ______    ______    ______

    38     'Male'      71        176       24.547
    43     'Male'      69        163       24.071
    38     'Female'    64        131       22.486
    40     'Female'    67        133       20.831
    49     'Female'    64        119       20.426

T has 100 rows and 5 variables.

See Also

| | | |

Related Examples

Was this topic helpful?