Main Content

removevars

Delete variables from table or timetable

Description

example

T2 = removevars(T1,vars) deletes the table variables specified by vars and copies the remaining variables to T2. You can specify variables by name, by position, or using logical indices.

For example, to remove table variable var3, use T2 = removevars(T1,'var3').

Examples

collapse all

Create a table and remove variables one at a time. You can specify variables by name or by position in the table.

Read data from a spreadsheet into a table. Display the first three rows.

T1 = readtable('outages.csv');
head(T1,3)
       Region             OutageTime          Loss     Customers       RestorationTime            Cause      
    _____________    ____________________    ______    __________    ____________________    ________________

    {'SouthWest'}    01-Feb-2002 12:18:00    458.98    1.8202e+06    07-Feb-2002 16:50:00    {'winter storm'}
    {'SouthEast'}    23-Jan-2003 00:49:00    530.14    2.1204e+05                     NaT    {'winter storm'}
    {'SouthEast'}    07-Feb-2003 21:15:00     289.4    1.4294e+05    17-Feb-2003 08:14:00    {'winter storm'}

Remove the variable that is named Region.

T2 = removevars(T1,'Region');
head(T2,3)
         OutageTime          Loss     Customers       RestorationTime            Cause      
    ____________________    ______    __________    ____________________    ________________

    01-Feb-2002 12:18:00    458.98    1.8202e+06    07-Feb-2002 16:50:00    {'winter storm'}
    23-Jan-2003 00:49:00    530.14    2.1204e+05                     NaT    {'winter storm'}
    07-Feb-2003 21:15:00     289.4    1.4294e+05    17-Feb-2003 08:14:00    {'winter storm'}

Remove the fourth variable from T2.

T3 = removevars(T2,4);
head(T3,3)
         OutageTime          Loss     Customers          Cause      
    ____________________    ______    __________    ________________

    01-Feb-2002 12:18:00    458.98    1.8202e+06    {'winter storm'}
    23-Jan-2003 00:49:00    530.14    2.1204e+05    {'winter storm'}
    07-Feb-2003 21:15:00     289.4    1.4294e+05    {'winter storm'}

Remove multiple table variables using the removevars function. You can specify variables by name or by position.

Read data from a spreadsheet into a table.

T1 = readtable('outages.csv');
head(T1,3)
       Region             OutageTime          Loss     Customers       RestorationTime            Cause      
    _____________    ____________________    ______    __________    ____________________    ________________

    {'SouthWest'}    01-Feb-2002 12:18:00    458.98    1.8202e+06    07-Feb-2002 16:50:00    {'winter storm'}
    {'SouthEast'}    23-Jan-2003 00:49:00    530.14    2.1204e+05                     NaT    {'winter storm'}
    {'SouthEast'}    07-Feb-2003 21:15:00     289.4    1.4294e+05    17-Feb-2003 08:14:00    {'winter storm'}

Remove the variables named Loss and Customers. Specify names using a cell array of character vectors.

T2 = removevars(T1,{'Loss','Customers'});
head(T2,3)
       Region             OutageTime           RestorationTime            Cause      
    _____________    ____________________    ____________________    ________________

    {'SouthWest'}    01-Feb-2002 12:18:00    07-Feb-2002 16:50:00    {'winter storm'}
    {'SouthEast'}    23-Jan-2003 00:49:00                     NaT    {'winter storm'}
    {'SouthEast'}    07-Feb-2003 21:15:00    17-Feb-2003 08:14:00    {'winter storm'}

Remove the first and fourth variables, using a numeric array to indicate their positions in T2.

T3 = removevars(T2,[1 4]);
head(T3,3)
         OutageTime           RestorationTime   
    ____________________    ____________________

    01-Feb-2002 12:18:00    07-Feb-2002 16:50:00
    23-Jan-2003 00:49:00                     NaT
    07-Feb-2003 21:15:00    17-Feb-2003 08:14:00

Input Arguments

collapse all

Input table, specified as a table or timetable.

Variables in the input table, specified as a string array, character vector, cell array of character vectors, pattern scalar, numeric array, or logical array.

Example: T2 = removevars(T1,2) removes the second table variable.

Example: T2 = removevars(T1,'Date') removes the table variable named Date.

Example: T2 = removevars(T1,{'Latitude','Longitude','Elevation'}) removes the table variables named Latitude, Longitude, and Elevation.

Extended Capabilities

Version History

Introduced in R2018a