MATLAB Answers

4

Is it possible to rename columns in a table?

Asked by Chelsea on 18 Feb 2015
Latest activity Edited by per isakson
on 2 Dec 2018
Hi,
I'm hoping there's a simple way to rename columns in a table. I don't need anything complicated, I'd just like to rename, for example, the 2nd, 7th, and 16th columns of a table - inside a function.
Thanks!

  0 Comments

Sign in to comment.

5 Answers

Answer by per isakson
on 18 Feb 2015
 Accepted Answer

Example
%%Create a table
T = table(['M';'M';'F';'F';'F'],[38;43;38;40;49],...
[71;69;64;67;64],[176;163;131;133;119])
T =
Var1 Var2 Var3 Var4
____ ____ ____ ____
M 38 71 176
M 43 69 163
F 38 64 131
F 40 67 133
F 49 64 119
%%Modify variable names
T.Properties.VariableNames = {'Gender' 'Age' 'Height' 'Weight'}
T =
Gender Age Height Weight
______ ___ ______ ______
M 38 71 176
M 43 69 163
F 38 64 131
F 40 67 133
F 49 64 119

  6 Comments

Expanding slightly: there is a VariableDescriptions property, normally empty. However, the only place it is used is for summary(). For all other displays of a table, it is the variable name that is displayed.
Hello @per isakson ! I have an issue in here ! actually I have these columns names to generate to the table :
T.Properties.VariableNames = {'Date_Time' 'Latitude' 'Longitude' 'Course' 'Speed' 'Temp_SBE45' 'Cond_SBE45' 'SoundVel_SBE45' 'full' 'pH_SeaFET' 'Saturation' 'Salinity_SBE45' 'Temperature_Optode' 'flow_pCO2' 'pH_Meinsberg' 'Temp_in_SBE38' 'Temp_Meinsberg' 'Oxygen' 'pCO2' 'pressure' 'flow_in' 'flow_main' 'flow_pH' 'Turbidity' 'halffull' 'Chl_a' 'Variance' 'pH_Meinsberg' 'Variance' 'Temp_Meinsberg' 'Variance' 'pH_SeaFET' 'Variance' 'pCO2' 'Variance' 'pressure' 'Variance' 'flow_in' 'Variance' 'flow_main' 'Variance' 'flow_pH' 'Variance' 'flow_pCO2' 'Variance' 'halffull' 'Variance' 'full' 'Variance'}
But I always get an error message :
Duplicate variable name: 'Temperature_Optode'.
Any Ideas ?
@Silver, this is a new question rather than a comment. Posting it as such increases the chances to get an answer.
There are many duplicates in that row. Obviously, duplicates are not allowed (although it's not clearly stated in the documentation(?)). However, 'Temperature_Optode' is not a duplicate.

Sign in to comment.


Answer by Peter Perkins
on 19 Feb 2015

The following are also possible:
Rename only some variables (note the parentheses on the left-hand side and the cell array of strings on the right-hand side) ...
>> T.Properties.VariableNames([1 3]) = {'Gender' 'Height'}
T =
Gender Var2 Height Var4
______ ____ ______ ____
M 38 71 176
M 43 69 163
F 38 64 131
F 40 67 133
F 49 64 119
... or rename only one variable (note the braces on the LHS instead of parentheses, and the raw string on the RHS) ...
>> T.Properties.VariableNames{2} = 'Age'
T =
Gender Age Height Var4
______ ___ ______ ____
M 38 71 176
M 43 69 163
F 38 64 131
F 40 67 133
F 49 64 119
... or rename a variable when you only know its (old) name.
>> T.Properties.VariableNames{'Var4'} = 'Weight'
T =
Gender Age Height Weight
______ ___ ______ ______
M 38 71 176
M 43 69 163
F 38 64 131
F 40 67 133
F 49 64 119

  2 Comments

Thank you :)
Actually you are renaming variables here, not column (headers) as requested in OP. Is there a way to just rename headers and not the actual variable names ?

Sign in to comment.


Answer by Oleg Komarov on 22 Apr 2015

My tableutils() has renameVarNames() that does exactly that with added flexibility.
The description is usually updated on https://github.com/okomarov/tableutils

  0 Comments

Sign in to comment.


Answer by Martin Patz on 21 Jul 2017

I agree with Cbhihe's comment above, the proposed solutions do not directly answer the OP's question. I face a case where the differentiation between column header variable is indeed important. For instance
v1 = [1:3]';
v2 = [2:4]';
m = [v1, v2];
t1 = table(v1, v2)
t2 = table(m)
gives two different results:
t1 =
v1 v2
__ __
1 2
2 3
3 4
t2 =
m
______
1 2
2 3
3 4
with previously posted answers it is not possible to rename the columns of t2 by specifying 'VariableNames', {'column_1', 'column_2'} as additional arguments to the table command. However you can split up a matrix into separate columns, which are then configureable, using the array2table command.
Example:
t3 = array2table(m, 'VariableNames', {'column_1', 'column_2'})
column_1 column_2
________ ________
1 2
2 3
3 4

  2 Comments

I disagree. t2 has only one column, which happens to be a 3 x 2 array.
... Any other interpretation would require that you be able to give separate column headers for each character in variable-length strings.
This is the very reason why we try to refer to the vertical things in a table as "variables", not as "columns".
Martin, m in your example is an Nx2 double. Double arrays have no way to "name" their columns, and you are correct that just putting an Nx2 double into a table does not add that capability. You might do one of two things:
1) As you say, it's pretty easy to split the Nx2 into two Nx1 column vectors. But presumably, there's some reason why you'd want m as an Nx2 rather than splitting it.
2) You could make m itself a table with two variables, and out that in a table. That would be kind of pointless if the outer table only had m as its one variable, but in cases wher you have a table with several variables, one of which has multiple columns, it can be useful. Currently, the display for a "table in a table" is not very helpful, but all of the subscripting and so on works as you'd expect. On the other hand, a table is not a double, so while this strategy keeps those two columns "together" in some sense, you might want to work with m as a double.

Sign in to comment.


Answer by Mav Sam
on 18 May 2018

terrible!

  0 Comments

Sign in to comment.