Delete variables from table or timetable
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)
ans=3×6 table
Region OutageTime Loss Customers RestorationTime Cause
_____________ ________________ ______ __________ ________________ ________________
{'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm'}
{'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm'}
{'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm'}
Remove the variable that is named Region
.
T2 = removevars(T1,'Region');
head(T2,3)
ans=3×5 table
OutageTime Loss Customers RestorationTime Cause
________________ ______ __________ ________________ ________________
2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm'}
2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm'}
2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm'}
Remove the fourth variable from T2
.
T3 = removevars(T2,4); head(T3,3)
ans=3×4 table
OutageTime Loss Customers Cause
________________ ______ __________ ________________
2002-02-01 12:18 458.98 1.8202e+06 {'winter storm'}
2003-01-23 00:49 530.14 2.1204e+05 {'winter storm'}
2003-02-07 21:15 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)
ans=3×6 table
Region OutageTime Loss Customers RestorationTime Cause
_____________ ________________ ______ __________ ________________ ________________
{'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm'}
{'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm'}
{'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'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)
ans=3×4 table
Region OutageTime RestorationTime Cause
_____________ ________________ ________________ ________________
{'SouthWest'} 2002-02-01 12:18 2002-02-07 16:50 {'winter storm'}
{'SouthEast'} 2003-01-23 00:49 NaT {'winter storm'}
{'SouthEast'} 2003-02-07 21:15 2003-02-17 08:14 {'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)
ans=3×2 table
OutageTime RestorationTime
________________ ________________
2002-02-01 12:18 2002-02-07 16:50
2003-01-23 00:49 NaT
2003-02-07 21:15 2003-02-17 08:14
T1
— Input tableInput table, specified as a table or timetable.
vars
— Variables in input tableVariables in the input table, specified as a character vector, cell array of character vectors, string array, 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
.
This function fully supports tall arrays. For more information, see Tall Arrays.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
addvars
| mergevars
| movevars
| renamevars
| splitvars
You have a modified version of this example. Do you want to open this example with your edits?