This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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);
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.


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

T2 = readtable('morePatients.txt');
Tnew = [T;T2];
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',...
T2 = cell2table(cellPatients(2:end,:));
T2.Properties.VariableNames = cellPatients(1,:);

Tnew = [Tnew;T2];
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)];
ans =

   108     8

Omit Duplicate Rows

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

Tnew = unique(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],:) = [];
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',:) = [];
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,:) = [];
ans =

    85     7

The table now has 17 fewer rows.

See Also

| | | | |

Related Examples

Was this topic helpful?