Main Content

removevars

从表或时间表中删除变量

说明

示例

T2 = removevars(T1,vars) 删除由 vars 指定的表变量,并将其余变量复制到 T2 中。您可以按名称、按位置或使用逻辑索引来指定变量。

例如,要删除表变量 var3,请使用 T2 = removevars(T1,'var3')

示例

全部折叠

创建一个表并逐个删除变量。您可以通过名称或表中的位置指定变量。

将数据从电子表格读取到表中。显示前三行。

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'}

删除名为 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'}

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'}

使用 removevars 函数删除多个表变量。您可以通过名称或位置指定变量。

将数据从电子表格读取到表中。

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'}

删除名为 LossCustomers 的变量。使用字符向量元胞数组指定名称。

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'}

删除第一个和第四个变量(使用数值数组指示它们在 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

输入参数

全部折叠

输入表,指定为表或时间表。

输入表中的变量,指定为字符串数组、字符向量、字符向量元胞数组、pattern 标量、数值数组或逻辑数组。

示例: T2 = removevars(T1,2) 删除第二个表变量。

示例: T2 = removevars(T1,'Date') 删除名为 Date 的表变量。

示例: T2 = removevars(T1,{'Latitude','Longitude','Elevation'}) 删除名为 LatitudeLongitudeElevation 的表变量。

扩展功能

版本历史记录

在 R2018a 中推出