movevars - Move variables in table or timetable - MATLAB (original) (raw)
Move variables in table or timetable
Syntax
Description
T2 = movevars([T1](#d126e1129933),[vars](#d126e1129952))
moves the specified variables to the end of the input table. The end is the variable farthest to the right. (since R2023a)
For example, to move a table variable named var3
to the end ofT1
, use T2 = movevars(T1,'var3')
. If the last variable of T1
is named var5
, then this syntax moves var3
to the right of var5
.
T2 = movevars([T1](#d126e1129933),[vars](#d126e1129952),'After',[location](#d126e1129986))
moves the specified table variables to the right of the table variable indicated bylocation
. You can specify variables andlocation
by name, by position, or by an array of logical indices. The variable specified by location
can be any variable in the input table.
For example, to move a table variable named var3
after table variable var5
, use T2 = movevars(T1,'var3','After','var5')
.
- Before R2023a: If you do not know the name or position of the last variable, you can move
vars
to the end of the table by using the syntaxT2 = movevars(T1,vars,'After',width(T1))
. The width function returns the number of variables in a table.
T2 = movevars([T1](#d126e1129933),[vars](#d126e1129952),'Before',[location](#d126e1129986))
moves the table variables specified by vars
to the left of the variable specified by location
.
- To move
vars
to beginning of the table, useT2 = movevars(T1,vars,'Before',1)
. The beginning is to the left of the first table variable.
Examples
Create a table and move 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'} 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'}
Move the variable that is named Region
so that it is before the variable named Cause
.
T2 = movevars(T1,'Region','Before','Cause'); head(T2,3)
OutageTime Loss Customers RestorationTime Region Cause
________________ ______ __________ ________________ _____________ ________________
2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'SouthWest'} {'winter storm'}
2003-01-23 00:49 530.14 2.1204e+05 NaT {'SouthEast'} {'winter storm'}
2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'SouthEast'} {'winter storm'}
Move the fourth variable so that it is after the first variable.
T3 = movevars(T2,4,'After',1); head(T3,3)
OutageTime RestorationTime Loss Customers Region Cause
________________ ________________ ______ __________ _____________ ________________
2002-02-01 12:18 2002-02-07 16:50 458.98 1.8202e+06 {'SouthWest'} {'winter storm'}
2003-01-23 00:49 NaT 530.14 2.1204e+05 {'SouthEast'} {'winter storm'}
2003-02-07 21:15 2003-02-17 08:14 289.4 1.4294e+05 {'SouthEast'} {'winter storm'}
Move multiple table variables using the movevars
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'} 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'}
Move the variables named Loss
, Customer
, and Cause
so that they are before the first variable. Specify names using a cell array of character vectors.
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'} 2002-02-01 12:18 2002-02-07 16:50
530.14 2.1204e+05 {'winter storm'} {'SouthEast'} 2003-01-23 00:49 NaT
289.4 1.4294e+05 {'winter storm'} {'SouthEast'} 2003-02-07 21:15 2003-02-17 08:14
Move the first four variables of T2
so that they are after RestorationTime
.
T3 = movevars(T2,[1:4],'After','RestorationTime'); head(T3,3)
OutageTime RestorationTime Loss Customers Cause Region
________________ ________________ ______ __________ ________________ _____________
2002-02-01 12:18 2002-02-07 16:50 458.98 1.8202e+06 {'winter storm'} {'SouthWest'}
2003-01-23 00:49 NaT 530.14 2.1204e+05 {'winter storm'} {'SouthEast'}
2003-02-07 21:15 2003-02-17 08:14 289.4 1.4294e+05 {'winter storm'} {'SouthEast'}
Input Arguments
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.
Location to insert moved variables, specified as a character vector, string scalar, integer, or logical array.
- If
location
is a character vector or string scalar, then it is the name of a variable in the input tableT1
. - If
location
is the integern
, then it specifies then
th variable inT1
. - If
location
is a logical array, whosen
th element is1
(true
), then it specifies then
th variable inT1
. All other elements oflocation
must be0
(false
).
Extended Capabilities
Themovevars
function fully supports tall arrays. For more information, see Tall Arrays.
Version History
Introduced in R2018a
You can now move variables after the last variable without specifying theAfter
name-value argument. In previous releases, callingmovevars
without specifying either theAfter
or Before
name-value argument resulted in an error.