table

Methods

Method Reference: table.renamevars

table: tblB = renamevars (tblA, vars, newNames)

Rename variables in a table.

tblB = renamevars (tblA, vars, newNames) renames the selected variables in the table tblA specified by vars using the names in newNames.

vars can be any of the following types.

  • a character vector specifying a single variable.
  • a cell array of character vectors specifying a single or multiple variables.
  • a string array specifying a single or multiple variables.
  • a numeric array of integer values indexing the variables to be renamed.
  • a logical vector of the same length as the width of the table tblA indexing as true the variables to be renamed.
  • a vartype object used to create a subscript that selects variables of a specified type.

newNames can either be a character vector (when renaming a single variable) or a cell array of character vectors or a string array. The number of names specified by newNames must match the number of variables specified by vars.

Source Code: table

Example: 1

renamevars changes variable names in place, matching old names to new ones position by position. It touches only the names — the data, of whatever type, is untouched.

 Nm = string ({'Sanchez'; 'Johnson'; 'Li'});
 Yrs = [38; 43; 40];
 Sex = categorical ({'M'; 'M'; 'F'});
 T = table (Nm, Yrs, Sex)
T =
  3x3 table

       Nm        Yrs    Sex    
    _________    ___    ___    

    "Sanchez"     38      M    
    "Johnson"     43      M    
    "Li"          40      F
 renamevars (T, {'Nm', 'Yrs', 'Sex'}, {'Name', 'Age', 'Gender'})
ans =
  3x3 table

      Name       Age    Gender    
    _________    ___    ______    

    "Sanchez"     38         M    
    "Johnson"     43         M    
    "Li"          40         F