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.
true the variables to be renamed.
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
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