Add and Delete Table Rows

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

Load Sample Data

Load the sample patients data and create a table, T.

load patients

T = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
size(T)
ans =

   100     8

The table, T, has 100 rows and 8 variables (columns).

Add Rows by Concatenation

Create a comma-delimited file, morePatients.txt, with the following additional patient data.

LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic
Abbot,Female,31,65,156,1,128,85
Bailey,Female,38,68,130,0,113,81
Cho,Female,35,61,130,0,124,80
Daniels,Female,48,67,142,1,123,74

Append the rows in the file to the end of the table, T.

T2 = readtable('morePatients.txt');
Tnew = [T;T2];
size(Tnew)
ans =

   104     8

The table Tnew has 104 rows. In order to vertically concatenate two tables, both tables must have the same number of variables, with the same variable names. If the variable names are different, you can directly assign new rows in a table to rows from another table. For example, T(end+1:end+4,:) = T2.

Add Rows from a Cell Array

If you want to append new rows stored in a cell array, first convert the cell array to a table, and then concatenate the tables.

cellPatients = {'LastName','Gender','Age','Height','Weight',...
    'Smoker','Systolic','Diastolic';
    'Edwards','Male',42,70,158,0,116,83;
    'Falk','Female',28,62,125,1,120,71};
T2 = cell2table(cellPatients(2:end,:));
T2.Properties.VariableNames = cellPatients(1,:);

Tnew = [Tnew;T2];
size(Tnew)
ans =

   106     8

Add Rows from a Structure

You also can append new rows stored in a structure. Convert the structure to a table, and then concatenate the tables.

structPatients(1,1).LastName = 'George';
structPatients(1,1).Gender = 'Male';
structPatients(1,1).Age = 45;
structPatients(1,1).Height = 76;
structPatients(1,1).Weight = 182;
structPatients(1,1).Smoker = 1;
structPatients(1,1).Systolic = 132;
structPatients(1,1).Diastolic = 85;

structPatients(2,1).LastName = 'Hadley';
structPatients(2,1).Gender = 'Female';
structPatients(2,1).Age = 29;
structPatients(2,1).Height = 58;
structPatients(2,1).Weight = 120;
structPatients(2,1).Smoker = 0;
structPatients(2,1).Systolic = 112;
structPatients(2,1).Diastolic = 70;

Tnew = [Tnew;struct2table(structPatients)];
size(Tnew)
ans =

   108     8

Omit Duplicate Rows

Use unique to omit any rows in a table that are duplicated.

Tnew = unique(Tnew);
size(Tnew)
ans =

   106     8

Two duplicated rows are deleted.

Delete Rows by Row Number

Delete rows 18, 20, and 21 from the table.

Tnew([18,20,21],:) = [];
size(Tnew)
ans =

   103     8

The table contains information on 103 patients now.

Delete Rows by Row Name

First, specify the variable of identifiers, LastName, as row names. Then, delete the variable, LastName, from Tnew. Finally, use the row name to index and delete rows.

Tnew.Properties.RowNames = Tnew.LastName;
Tnew.LastName = [];
Tnew('Smith',:) = [];
size(Tnew)
ans =

   102     7

The table now has one less row and one less variable.

Search for Rows To Delete

You also can search for observations in the table. For example, delete rows for any patients under the age of 30.

toDelete = Tnew.Age<30;
Tnew(toDelete,:) = [];
size(Tnew)
ans =

    85     7

The table now has 17 fewer rows.

See Also

| | | | |

Related Examples

Was this topic helpful?