Main Content

movevars

在表或时间表中移动变量

说明

T2 = movevars(T1,vars) 将指定的变量移至输入表的末尾。末尾是最右侧的变量。 (自 R2023a 起)

例如,要将名为 var3 的表变量移至 T1 的末尾,请使用 T2 = movevars(T1,'var3')。如果 T1 的最后一个变量命名为 var5,则此语法将 var3 移到 var5 的右侧。

示例

T2 = movevars(T1,vars,'After',location) 将指定的表变量移动到由 location 指示的表变量的右侧。您可以通过名称、位置或逻辑索引数组来指定变量和 locationlocation 指定的变量可以是输入表中的任何变量。

例如,要将名为 var3 的表变量移至表变量 var5 后,请使用 T2 = movevars(T1,'var3','After','var5')

  • 在 R2023a 之前: 如果不知道最后一个变量的名称或位置,可以使用语法 T2 = movevars(T1,vars,'After',width(T1))vars 移到表的末尾。width 函数返回表中变量的数目。

示例

T2 = movevars(T1,vars,'Before',location) 将由 vars 指定的表变量移动到由 location 指定的变量的左侧。

  • 要将 vars 移至表的开头,请使用 T2 = movevars(T1,vars,'Before',1)。开头位于第一个表变量的左侧。

示例

全部折叠

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

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

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 的变量,使其位于名为 Cause 的变量之前。

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

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

移动第四个变量,使其位于第一个变量之后。

T3 = movevars(T2,4,'After',1);
head(T3,3)
         OutageTime           RestorationTime        Loss     Customers        Region             Cause      
    ____________________    ____________________    ______    __________    _____________    ________________

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

使用 movevars 函数移动多个表变量。您可以通过名称或位置指定变量。

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

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

移动名为 LossCustomerCause 的变量,使其位于第一个变量之前。使用字符向量元胞数组指定名称。

T2 = movevars(T1,{'Loss','Customers','Cause'},'Before',1);
head(T2,3)
     Loss     Customers          Cause             Region             OutageTime           RestorationTime   
    ______    __________    ________________    _____________    ____________________    ____________________

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

移动 T2 的前四个变量,使它们位于 RestorationTime 之后。

T3 = movevars(T2,[1:4],'After','RestorationTime');
head(T3,3)
         OutageTime           RestorationTime        Loss     Customers          Cause             Region    
    ____________________    ____________________    ______    __________    ________________    _____________

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

输入参数

全部折叠

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

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

要插入所移动的变量的位置,指定为字符向量、字符串标量、整数或逻辑数组。

  • 如果 location 是字符向量或字符串标量,则它是输入表 T1 中变量的名称。

  • 如果 location 是整数 n,则它指定 T1 中的第 n 个变量。

  • 如果 location 是逻辑数组,其第 n 个元素为 1 (true),则它指定 T1 中的第 n 个变量。location 的所有其他元素必须为 0 (false)。

扩展功能

版本历史记录

在 R2018a 中推出

全部展开