movevars
在表或时间表中移动变量
语法
说明
T2 = movevars(
将指定的表变量移动到由 T1
,vars
,'After',location
)location
指示的表变量的右侧。您可以通过名称、位置或逻辑索引数组来指定变量和 location
。location
指定的变量可以是输入表中的任何变量。
例如,要将名为 var3
的表变量移至表变量 var5
后,请使用 T2 = movevars(T1,'var3','After','var5')
。
在 R2023a 之前: 如果不知道最后一个变量的名称或位置,可以使用语法
T2 = movevars(T1,vars,'After',width(T1))
将vars
移到表的末尾。width
函数返回表中变量的数目。
示例
移动变量
创建一个表并逐个移动变量。您可以通过名称或表中的位置指定变量。
将数据从电子表格读取到表中。显示前三行。
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'}
移动名为 Loss
、Customer
和 Cause
的变量,使其位于第一个变量之前。使用字符向量元胞数组指定名称。
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'}
输入参数
T1
— 输入表
表 | 时间表
输入表,指定为表或时间表。
vars
— 输入表中的变量
字符串数组 | 字符向量 | 字符向量元胞数组 | pattern
标量 | 数值数组 | 逻辑数组
输入表中的变量,指定为字符串数组、字符向量、字符向量元胞数组、pattern
标量、数值数组或逻辑数组。
location
— 插入所移动变量的位置
字符向量 | 字符串标量 | 整数 | 逻辑数组
要插入所移动的变量的位置,指定为字符向量、字符串标量、整数或逻辑数组。
如果
location
是字符向量或字符串标量,则它是输入表T1
中变量的名称。如果
location
是整数n
,则它指定T1
中的第n
个变量。如果
location
是逻辑数组,其第n
个元素为1
(true
),则它指定T1
中的第n
个变量。location
的所有其他元素必须为0
(false
)。
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
vars
输入参数不能包含重复的变量名称。vars
输入参数不支持模式表达式。
有关详细信息,请参阅 表的代码生成 (MATLAB Coder) 和 Table Limitations for Code Generation (MATLAB Coder)。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2018a 中推出R2023a: 将表变量移至不带 After
名称-值参数的最后一个变量后
现在可以将变量移至最后一个变量后,而无需指定 After
名称-值参数。在以前的版本中,在未指定 After
或 Before
名称-值参数的情况下调用 movevars
会导致错误。
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)