Main Content

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 = 1×2

   100     8

The table, T, has 100 rows and eight variables.

Add Rows by Concatenation

Read data on more patients from a comma-delimited file, morePatients.csv, into a table, T2. Then, append the rows from T2 to the end of the table, T.

T2 = readtable('morePatients.csv');
Tnew = [T;T2];
size(Tnew)
ans = 1×2

   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 Cell Array

To append new rows stored in a cell array, vertically concatenate the cell array onto the end of the table. You can concatenate directly from a cell array when it has the right number of columns and the contents of its cells can be concatenated onto the corresponding table variables.

cellPatients = {'Trujillo','Male',42,70,158,0,116,83;
                'Falk','Female',28,62,125,1,120,71};
Tnew = [Tnew;cellPatients];
size(Tnew)
ans = 1×2

   106     8

You also can convert a cell array to a table using the cell2table function.

Add Rows from 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 = 'Nonbinary';
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 = 'Russo';
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 = 1×2

   108     8

Omit Duplicate Rows

To omit any rows in a table that are duplicated, use the unique function.

Tnew = unique(Tnew);
size(Tnew)
ans = 1×2

   107     8

unique deleted two duplicate rows.

Delete Rows by Row Number

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

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

   104     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 = 1×2

   103     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 = 1×2

    86     7

The table now has 17 fewer rows.

See Also

| | | |

Related Topics