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"    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"   
    "West"         06-Apr-2004 05:44:00    434.81    3.4037e+05    06-Apr-2004 06:10:00    "equipment fault"
    "MidWest"      16-Mar-2002 06:18:00    186.44    2.1275e+05    18-Mar-2002 23:23:00    "severe storm"   
    "West"         18-Jun-2003 02:49:00         0             0    18-Jun-2003 10:54:00    "attack"         
    "West"         20-Jun-2004 14:39:00    231.29           NaN    20-Jun-2004 19:16:00    "equipment fault"
    "West"         06-Jun-2002 19:28:00    311.86           NaN    07-Jun-2002 00:51:00    "equipment fault"
    "NorthEast"    16-Jul-2003 16:23:00    239.93         49434    17-Jul-2003 01:12:00    "fire"           
    "MidWest"      27-Sep-2004 11:09:00    286.72         66104    27-Sep-2004 16:37:00    "equipment fault"
    "SouthEast"    05-Sep-2004 17:48:00    73.387         36073    05-Sep-2004 20:46:00    "equipment fault"
    "West"         21-May-2004 21:45:00    159.99           NaN    22-May-2004 04:23:00    "equipment fault"
    "SouthEast"    01-Sep-2002 18:22:00    95.917         36759    01-Sep-2002 19:12:00    "severe storm"   
    "SouthEast"    27-Sep-2003 07:32:00       NaN    3.5517e+05    04-Oct-2003 07:02:00    "severe storm"   
    "West"         12-Nov-2003 06:12:00    254.09    9.2429e+05    17-Nov-2003 02:04:00    "winter storm"   
    "NorthEast"    18-Sep-2004 05:54:00         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"    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"   
    "West"         06-Apr-2004 05:44:00      434.81      3.4037e+05    06-Apr-2004 06:10:00    "equipment fault"
    "MidWest"      16-Mar-2002 06:18:00      186.44      2.1275e+05    18-Mar-2002 23:23:00    "severe storm"   
    "West"         18-Jun-2003 02:49:00           0               0    18-Jun-2003 10:54:00    "attack"         
    "West"         20-Jun-2004 14:39:00      231.29             NaN    20-Jun-2004 19:16:00    "equipment fault"
    "West"         06-Jun-2002 19:28:00      311.86             NaN    07-Jun-2002 00:51:00    "equipment fault"
    "NorthEast"    16-Jul-2003 16:23:00      239.93           49434    17-Jul-2003 01:12:00    "fire"           
    "MidWest"      27-Sep-2004 11:09:00      286.72           66104    27-Sep-2004 16:37:00    "equipment fault"
    "SouthEast"    05-Sep-2004 17:48:00      73.387           36073    05-Sep-2004 20:46:00    "equipment fault"
    "West"         21-May-2004 21:45:00      159.99             NaN    22-May-2004 04:23:00    "equipment fault"
    "SouthEast"    01-Sep-2002 18:22:00      95.917           36759    01-Sep-2002 19:12:00    "severe storm"   
    "SouthEast"    27-Sep-2003 07:32:00         NaN      3.5517e+05    04-Oct-2003 07:02:00    "severe storm"   
    "West"         12-Nov-2003 06:12:00      254.09      9.2429e+05    17-Nov-2003 02:04:00    "winter storm"   
    "NorthEast"    18-Sep-2004 05:54:00           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