Main Content

movevars

Move variables in table or timetable

Description

T2 = movevars(T1,vars) 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 of T1, use T2 = movevars(T1,'var3'). If the last variable of T1 is named var5, then this syntax moves var3 to the right of var5.

example

T2 = movevars(T1,vars,'After',location) moves the specified table variables to the right of the table variable indicated by location. You can specify variables and location 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 syntax T2 = movevars(T1,vars,'After',width(T1)). The width function returns the number of variables in a table.

example

T2 = movevars(T1,vars,'Before',location) moves the table variables specified by vars to the left of the variable specified by location.

  • To move vars to beginning of the table, use T2 = movevars(T1,vars,'Before',1). The beginning is to the left of the first table variable.

Examples

collapse all

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

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      
    ____________________    ______    __________    ____________________    _____________    ________________

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

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      
    ____________________    ____________________    ______    __________    _____________    ________________

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

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

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

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    
    ____________________    ____________________    ______    __________    ________________    _____________

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

Input Arguments

collapse all

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 table T1.

  • If location is the integer n, then it specifies the nth variable in T1.

  • If location is a logical array, whose nth element is 1 (true), then it specifies the nth variable in T1. All other elements of location must be 0 (false).

Extended Capabilities

Version History

Introduced in R2018a

expand all