movevars
Move variables in table or timetable
Description
Examples
Move Variable
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'}
Specify Multiple Variables
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'}
Input Arguments
T1
— Input table
table | timetable
Input table, specified as a table or timetable.
vars
— Variables in input table
string array | character vector | cell array of character vectors | pattern
scalar | numeric array | logical array
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
— Location to insert moved variables
character vector | string scalar | integer | 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
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Usage notes and limitations:
The
vars
input argument does not support pattern expressions.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The
vars
input argument cannot contain duplicate variable names.The
vars
input argument does not support pattern expressions.
For more information, see Code Generation for Tables (MATLAB Coder) and Table Limitations for Code Generation (MATLAB Coder).
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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)