Main Content

Add and Delete Table Rows

This example shows how to add and delete rows in a table. You can add rows at the bottom, insert rows in the middle, or combine two tables. If you preallocate space for a table, you can fill it with data by replacing rows. You can delete rows by number, by name, or by condition, including duplicate rows or rows with missing values. For simple cases, you can modify a table interactively using the Variables Editor.

Create Sample Table

First, create a table from a comma-separated values (CSV) file using the readtable function. The sample file has simulated data about a small number of patients. The third row has missing values, indicating incomplete data for that patient.

patientSample1 = readtable("patientSample1.csv",TextType="string")
patientSample1=5×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Brown"       49       64       119        0             "Good"            
    "Martinez"    37       70       179        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Wright"      45       70       126        1             "Excellent"       
    "Kim"         41       65       127        0             "Poor"            

Add Row to End of Table

To add one row to the end of a table, use a one-row table or a one-row cell array. Add the new row by placing it outside the existing row index boundary.

For convenience, use a cell array with values for the new row. Include values for each table variable, with compatible data types.

oneRowCellArray = {"Lee" 55 73 167 false "Fair"}
oneRowCellArray=1×6 cell array
    {["Lee"]}    {[55]}    {[73]}    {[167]}    {[0]}    {["Fair"]}

To add the row to the bottom of the table, specify end+1 as the row index. In general, the end keyword specifies the last element of an array. When used to specify the row index of an array or table, end specifies the last row. The assignment extracts values from the cell array and assigns them into the new row of patientSample1.

patientSample1(end+1,:) = oneRowCellArray
patientSample1=6×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Brown"       49       64       119        0             "Good"            
    "Martinez"    37       70       179        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Wright"      45       70       126        1             "Excellent"       
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            

Similarly, you can use a one-row table, either by creating a new table or specifying one row of an existing table.

Create a one-row table.

oneRowTable = table("Griffin",49,70,186,false,"Fair")
oneRowTable=1×6 table
      Var1       Var2    Var3    Var4    Var5      Var6 
    _________    ____    ____    ____    _____    ______

    "Griffin"     49      70     186     false    "Fair"

When you assign a row using this syntax, the names of the variables of patientSample1 and oneRowTable can be different. In this syntax, the assignment extracts values from oneRowTable and assigns them into the new row of patientSample1.

patientSample1(end+1,:) = oneRowTable
patientSample1=7×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Brown"       49       64       119        0             "Good"            
    "Martinez"    37       70       179        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Wright"      45       70       126        1             "Excellent"       
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            
    "Griffin"     49       70       186        0             "Fair"            

Replace Existing Row

You can also assign values to a row that already exists. The assignment operation overwrites the row.

For example, assign oneRowCellArray to the second row of patientSample1. The assignment overwrites data for patient Martinez with data for patient Lee.

patientSample1(2,:) = oneRowCellArray
patientSample1=7×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    "Brown"      49       64       119        0             "Good"            
    "Lee"        55       73       167        0             "Fair"            
    "Chen"       55      NaN       NaN        1             <missing>         
    "Wright"     45       70       126        1             "Excellent"       
    "Kim"        41       65       127        0             "Poor"            
    "Lee"        55       73       167        0             "Fair"            
    "Griffin"    49       70       186        0             "Fair"            

Insert Row at Specific Position

To insert a row at a specific position between existing rows, split the table into two and use vertical concatenation. For example, insert oneRowCellArray between the third and fourth rows of the table.

patientSample1 = [patientSample1(1:3,:); oneRowCellArray; patientSample1(4:end,:)]
patientSample1=8×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    "Brown"      49       64       119        0             "Good"            
    "Lee"        55       73       167        0             "Fair"            
    "Chen"       55      NaN       NaN        1             <missing>         
    "Lee"        55       73       167        0             "Fair"            
    "Wright"     45       70       126        1             "Excellent"       
    "Kim"        41       65       127        0             "Poor"            
    "Lee"        55       73       167        0             "Fair"            
    "Griffin"    49       70       186        0             "Fair"            

Add Row and Sort Table

Another way to insert rows is to add rows to the end of a table and then reorder the table by using the sortrows function.

For example, add a row for another patient.

patientSample1(end+1,:) = {"Anderson" 45 68 128 false "Excellent"}
patientSample1=9×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Brown"       49       64       119        0             "Good"            
    "Lee"         55       73       167        0             "Fair"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            
    "Griffin"     49       70       186        0             "Fair"            
    "Anderson"    45       68       128        0             "Excellent"       

If your table is unordered, or if it becomes unordered when you add new rows, sort the table using sortrows. By default, sortrows sorts the table by the values in the first variable in ascending order. In this case, the sort order is by the last names of the patients.

patientSample1 = sortrows(patientSample1)
patientSample1=9×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Brown"       49       64       119        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Griffin"     49       70       186        0             "Fair"            
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       

Add Rows to Empty Table

You can start with an empty table and add rows of data to it. This method can be useful if you collect data over time and want to periodically add data to the end of a table.

First, create an empty table.

startFromEmpty = table
startFromEmpty =

  0×0 empty table

Next, expand the table by adding a row after the last row of the table.

startFromEmpty(end+1,:) = oneRowCellArray
startFromEmpty=1×6 table
    Var1     Var2    Var3    Var4    Var5      Var6 
    _____    ____    ____    ____    _____    ______

    "Lee"     55      73     167     false    "Fair"

To rename all the variables, assign an array of new variable names to the VariableNames property of the table.

varNames = ["LastName" "Age" "Height" "Weight" "Smoker" "SelfAssessedHealthStatus"];
startFromEmpty.Properties.VariableNames = varNames
startFromEmpty=1×6 table
    LastName    Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    ________    ___    ______    ______    ______    ________________________

     "Lee"      55       73       167      false              "Fair"         

Assign Rows to Preallocated Table

While you can start with an empty table and add rows one at a time, it can be more efficient to preallocate a table with many rows. Then you can fill in rows as data becomes available.

To preallocate a table, use the table function. Specify the size of the table, the names of the variables, and the data types of the variables. For example, preallocate a table with six rows and nine variables. Preallocation fills the variables with missing values that are appropriate for their data types.

varNames = ["LastName" "Age" "Height" "Weight" "Smoker" "SelfAssessedHealthStatus"];
varTypes = ["string" "double" "double" "double" "logical" "string"];
preallocatedTable = table(Size=[4 6],VariableNames=varNames,VariableTypes=varTypes)
preallocatedTable=4×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    <missing>     0       0         0       false            <missing>        
    <missing>     0       0         0       false            <missing>        
    <missing>     0       0         0       false            <missing>        
    <missing>     0       0         0       false            <missing>        

Add data to the first row. This assignment overwrites the missing values in the first row.

preallocatedTable(1,:) = oneRowCellArray
preallocatedTable=4×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    "Lee"        55       73       167      false            "Fair"           
    <missing>     0        0         0      false            <missing>        
    <missing>     0        0         0      false            <missing>        
    <missing>     0        0         0      false            <missing>        

Add Empty Rows

Starting in R2023b, you can preallocate more rows at the end of a table by using the resize function. The new rows are filled with missing values, which you can update later.

First, determine how many rows patientSample1 has by using the height function.

numberOfRows = height(patientSample1)
numberOfRows = 
9

Next, add three more rows using resize. The second argument to resize specifies the total number of rows in the resized table.

patientSample1 = resize(patientSample1,numberOfRows+3)
patientSample1=12×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Brown"       49       64       119        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Griffin"     49       70       186        0             "Fair"            
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         

Combine Two Tables

Another way to add rows to a table is to concatenate another table. When you vertically concatenate two tables, they must have the same number of variables with the same variable names. Matching variables from the top and bottom tables must have compatible data types and sizes. However, the two tables can have their variables in a different order because vertical concatenation matches variables by name. The concatenated table has variables in the same order as the top table.

For example, create a second table from another sample CSV file. The second table has the same variables as the first table.

patientSample2 = readtable("patientSample2.csv",TextType="string")
patientSample2=4×6 table
     LastName      Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    ___________    ___    ______    ______    ______    ________________________

    "Garcia"       27       69       131        1             "Fair"            
    "Murphy"       36       71       180        0             "Good"            
    "Takahashi"    29       63       130        0             "Excellent"       
    "Brown"        49       64       119        0             "Good"            

Vertically concatenate the two tables.

combinedPatients = [patientSample1; patientSample2]
combinedPatients=16×6 table
     LastName      Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    ___________    ___    ______    ______    ______    ________________________

    "Anderson"     45       68       128        0             "Excellent"       
    "Brown"        49       64       119        0             "Good"            
    "Chen"         55      NaN       NaN        1             <missing>         
    "Griffin"      49       70       186        0             "Fair"            
    "Kim"          41       65       127        0             "Poor"            
    "Lee"          55       73       167        0             "Fair"            
    "Lee"          55       73       167        0             "Fair"            
    "Lee"          55       73       167        0             "Fair"            
    "Wright"       45       70       126        1             "Excellent"       
    <missing>       0        0         0        0             <missing>         
    <missing>       0        0         0        0             <missing>         
    <missing>       0        0         0        0             <missing>         
    "Garcia"       27       69       131        1             "Fair"            
    "Murphy"       36       71       180        0             "Good"            
    "Takahashi"    29       63       130        0             "Excellent"       
    "Brown"        49       64       119        0             "Good"            

Delete Rows by Row Number

You can delete rows by using the row number as a subscript and assigning the empty array, [].

For example, delete rows 2 and 5 from the table.

patientSample1([2 5],:) = []
patientSample1=10×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Chen"        55      NaN       NaN        1             <missing>         
    "Griffin"     49       70       186        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         

Delete Duplicate Rows

You can also clean up a table by deleting duplicate rows.

For example, remove the duplicate rows for the patient named Lee by using the unique function. The function does not remove the rows at the end that are filled with missing values because missing values are considered unique. A missing value is not equal to any value, not even to itself.

patientSample1 = unique(patientSample1)
patientSample1=8×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Chen"        55      NaN       NaN        1             <missing>         
    "Griffin"     49       70       186        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         

Delete Rows with Missing Values

If a table has rows with missing values, you can delete them by using the rmmissing function.

For example, delete table rows that have missing values from the patient data table.

patientSample1 = rmmissing(patientSample1)
patientSample1=4×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Griffin"     49       70       186        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       

Alternatively, you can fill in missing values by using the fillmissing function.

Delete Rows That Meet Condition

You can also delete rows where values in one or more variables meet a condition.

For example, find the rows for patients who are 45 years old or younger. The <= operator returns a logical vector that you can use as a row subscript.

ageLessThanOrEqualTo45 = patientSample1.Age <= 45
ageLessThanOrEqualTo45 = 4×1 logical array

   1
   0
   0
   1

Delete the rows where Age is less than or equal to 45.

patientSample1(ageLessThanOrEqualTo45,:) = []
patientSample1=2×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    "Griffin"    49       70       186        0                "Fair"         
    "Lee"        55       73       167        0                "Fair"         

Delete Rows by Row Name

When a table has row names, you can use row names as subscripts. Specify row names for patientSample1. Then delete a row by its row name.

First, specify the variable of identifiers, LastName, as the row names. Then, delete the variable LastName from patientSample1.

patientSample1.Properties.RowNames = patientSample1.LastName;
patientSample1.LastName = []
patientSample1=2×5 table
               Age    Height    Weight    Smoker    SelfAssessedHealthStatus
               ___    ______    ______    ______    ________________________

    Griffin    49       70       186        0                "Fair"         
    Lee        55       73       167        0                "Fair"         

Delete a row by using the row name as a subscript and assigning the empty array, [].

patientSample1("Lee",:) = []
patientSample1=1×5 table
               Age    Height    Weight    Smoker    SelfAssessedHealthStatus
               ___    ______    ______    ______    ________________________

    Griffin    49       70       186        0                "Fair"         

See Also

Functions

Tools

Topics