This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

movevars

Move variables in table or timetable

Syntax

T2 = movevars(T1,vars,'Before',location)
T2 = movevars(T1,vars,'After',location)

Description

example

T2 = movevars(T1,vars,'Before',location) moves the table variables specified by vars to the left of the variable specified by location. You can specify variables and location by name, by position, or using logical indices.

T2 = movevars(T1,vars,'After',location) moves the variables to the right of the table variable indicated by location (see diagram).

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

Input Arguments

collapse all

Input table, specified as a table or timetable.

Variables in the input table, specified as a character vector, cell array of character vectors, string array, 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

Introduced in R2018a