Main Content

renamevars

Rename variables in table or timetable

Since R2020a

Description

example

T2 = renamevars(T1,vars,newNames) renames the table or timetable variables specified by vars using the names specified by newNames.

Examples

collapse all

Read a table from a spreadsheet. The spreadsheet provides a name for each table variable. (Read the columns containing text into table variables that are string arrays.)

T = readtable('outages.csv','TextType','string')
T=1468×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"   
    "West"         2004-04-06 05:44    434.81    3.4037e+05    2004-04-06 06:10    "equipment fault"
    "MidWest"      2002-03-16 06:18    186.44    2.1275e+05    2002-03-18 23:23    "severe storm"   
    "West"         2003-06-18 02:49         0             0    2003-06-18 10:54    "attack"         
    "West"         2004-06-20 14:39    231.29           NaN    2004-06-20 19:16    "equipment fault"
    "West"         2002-06-06 19:28    311.86           NaN    2002-06-07 00:51    "equipment fault"
    "NorthEast"    2003-07-16 16:23    239.93         49434    2003-07-17 01:12    "fire"           
    "MidWest"      2004-09-27 11:09    286.72         66104    2004-09-27 16:37    "equipment fault"
    "SouthEast"    2004-09-05 17:48    73.387         36073    2004-09-05 20:46    "equipment fault"
    "West"         2004-05-21 21:45    159.99           NaN    2004-05-22 04:23    "equipment fault"
    "SouthEast"    2002-09-01 18:22    95.917         36759    2002-09-01 19:12    "severe storm"   
    "SouthEast"    2003-09-27 07:32       NaN    3.5517e+05    2003-10-04 07:02    "severe storm"   
    "West"         2003-11-12 06:12    254.09    9.2429e+05    2003-11-17 02:04    "winter storm"   
    "NorthEast"    2004-09-18 05:54         0             0                 NaT    "equipment fault"
      ⋮

Change the names of the variables Loss, OutageTime, and RestorationTime.

T = renamevars(T,["Loss","OutageTime","RestorationTime"], ...
                 ["Total Cost","Start of Outage","Restoration"])
T=1468×6 table
      Region       Start of Outage     Total Cost    Customers       Restoration             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"   
    "West"         2004-04-06 05:44      434.81      3.4037e+05    2004-04-06 06:10    "equipment fault"
    "MidWest"      2002-03-16 06:18      186.44      2.1275e+05    2002-03-18 23:23    "severe storm"   
    "West"         2003-06-18 02:49           0               0    2003-06-18 10:54    "attack"         
    "West"         2004-06-20 14:39      231.29             NaN    2004-06-20 19:16    "equipment fault"
    "West"         2002-06-06 19:28      311.86             NaN    2002-06-07 00:51    "equipment fault"
    "NorthEast"    2003-07-16 16:23      239.93           49434    2003-07-17 01:12    "fire"           
    "MidWest"      2004-09-27 11:09      286.72           66104    2004-09-27 16:37    "equipment fault"
    "SouthEast"    2004-09-05 17:48      73.387           36073    2004-09-05 20:46    "equipment fault"
    "West"         2004-05-21 21:45      159.99             NaN    2004-05-22 04:23    "equipment fault"
    "SouthEast"    2002-09-01 18:22      95.917           36759    2002-09-01 19:12    "severe storm"   
    "SouthEast"    2003-09-27 07:32         NaN      3.5517e+05    2003-10-04 07:02    "severe storm"   
    "West"         2003-11-12 06:12      254.09      9.2429e+05    2003-11-17 02:04    "winter storm"   
    "NorthEast"    2004-09-18 05:54           0               0                 NaT    "equipment fault"
      ⋮

Create a table with many variables by using the array2table function. Add names for the variables.

T = array2table(rand(5,5))
T=5×5 table
     Var1       Var2       Var3       Var4        Var5  
    _______    _______    _______    _______    ________

    0.81472    0.09754    0.15761    0.14189     0.65574
    0.90579     0.2785    0.97059    0.42176    0.035712
    0.12699    0.54688    0.95717    0.91574     0.84913
    0.91338    0.95751    0.48538    0.79221     0.93399
    0.63236    0.96489    0.80028    0.95949     0.67874

Change the variable names so that they each start with "Reading" and end with a suffix. Determine how many variables T has by using the width function. Specify the table variables as a numeric array.

Convert the numeric array allVars to a string array. Create a 1-by-5 string array by appending each element to "Reading".

Rename all of the variables by using the renamevars function.

allVars = 1:width(T);
newNames = append("Reading",string(allVars));
T = renamevars(T,allVars,newNames)
T=5×5 table
    Reading1    Reading2    Reading3    Reading4    Reading5
    ________    ________    ________    ________    ________

    0.81472     0.09754     0.15761     0.14189      0.65574
    0.90579      0.2785     0.97059     0.42176     0.035712
    0.12699     0.54688     0.95717     0.91574      0.84913
    0.91338     0.95751     0.48538     0.79221      0.93399
    0.63236     0.96489     0.80028     0.95949      0.67874

Input Arguments

collapse all

Input table, specified as a table or a timetable.

Variables in the input table or timetable, specified as a character vector, string array, cell array of character vectors, numeric array, logical array, or subscript object.

You can specify vars as a subscript object that detects variables of a specified data type by using the vartype function.

Example: T = renamevars(T,'Var1','Location') changes the name of the table variable 'Var1' to 'Location'.

Example: T = renamevars(T,["Var1","Var2"],["Latitude","Longitude"]) changes the names of two table variables.

Example: T = renamevars(T,1:width(T),newNames) renames all of the table variables. The width of a table, returned by the width function, equals the number of its variables.

Example: vars = vartype('numeric'); T = renamevars(T,vars,newNames) renames all of the numeric variables in T.

New names for variables, specified as a character vector, string array, or cell array of character vectors.

The number of names specified by newNames must match the number of variables specified by vars.

Tips

  • You can also rename all of the variables in a table by setting its VariableNames property, as in T.Properties.VariableNames = newNames. In that case, newNames must be a string array or a cell array of character vectors.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2020a