Move variables in table or timetable
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)
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'}
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)
ans=3×6 table
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)
ans=3×6 table
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)
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'}
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)
ans=3×6 table
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)
ans=3×6 table
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'}
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.
location
— Location to insert moved variablesLocation 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 table
T1
.
If location
is the integer
n
, then it specifies the n
th
variable in T1
.
If location
is a logical array, whose
n
th element is 1
(true
), then it specifies the
n
th variable in T1
. All
other elements of location
must be
0
(false
).
This function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
The input argument vars
cannot contain duplicate
variable names.
For more information, see Code Generation for Tables (MATLAB Coder) and Table Limitations for Code Generation (MATLAB Coder).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
addvars
| mergevars
| removevars
| renamevars
| splitvars
You have a modified version of this example. Do you want to open this example with your edits?