Is it possible to rename columns in a table?
조회 수: 1,486 (최근 30일)
이전 댓글 표시
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!
댓글 수: 2
Ruxandra Gabriela Ionescu
2021년 9월 8일
You all wrote the same thing, the same example. I asked a question and you answered with an example. I don't examples! I need the solution in my case.. It's simple...
It's unuseful this page, sorry...
per isakson
2021년 9월 8일
"I don't examples! I need the solution in my case.. It's simple... It's unuseful this page, sorry..."
No it's not simple to understand what meaning you put in the word solution.
OP has accepted an answer, thus I believe OP found it at least marginally useful.
채택된 답변
per isakson
2022년 4월 28일
편집: MathWorks Support Team
2022년 4월 28일
You can rename the variables in a table by using either the "Properties.VariableNames" property or the "renamevars" function. (In a table, the column-oriented arrays of data are called variables, not columns, because variables can have multiple columns of their own.)
For example, consider this small table with default variable names.
T = table((1:3)',(4:6)')
T =
3×2 table
Var1 Var2
____ ____
1 4
2 5
3 6
Rename the variables by using “Properties.VariablesNames”.
T.Properties.VariableNames = ["A","B"]
T =
3×2 table
A B
_ _
1 4
2 5
3 6
Starting in R2020a, rename them by using “renamevars”.
T = table((1:3)',(4:6)');
T = renamevars(T,["Var1","Var2"],["Nums1","Nums2"])
T =
3×2 table
Nums1 Nums2
_____ _____
1 4
2 5
3 6
댓글 수: 6
Silver
2018년 10월 28일
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 ?
per isakson
2018년 12월 2일
편집: per isakson
2018년 12월 2일
@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.
추가 답변 (6개)
Peter Perkins
2015년 2월 19일
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
Campion Loong
2021년 10월 20일
Renaming table and timetable variables has become straightforward since R2020a:
Also check out the suite of convenient functions for table/timetable manipulations
댓글 수: 0
Oleg Komarov
2015년 4월 22일
The description is usually updated on https://github.com/okomarov/tableutils
댓글 수: 0
Martin Patz
2017년 7월 21일
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
Walter Roberson
2017년 7월 21일
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.
Peter Perkins
2017년 7월 21일
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.
Ruxandra Gabriela Ionescu
2021년 9월 6일
I have matrix B=[10 11 12 13 14; 15 16 17 18 19; 20 21 22 23 24; 25 26 27 28 29; 30 31 32 33 34].
- How can I set the name of the columns with ab, bc, cd, de and ef?
- How can I add a new column "fg" with the value 100?
- How can I set the values from cd and lines 1,2,3 at NaN value? I don't know what is NaN.
Thank you!
댓글 수: 2
per isakson
2021년 9월 8일
This is a new question and posting it as such increases the chances to get an answer.
Walter Roberson
2021년 9월 8일
Sounds like homework to me,
B=[10 11 12 13 14; 15 16 17 18 19; 20 21 22 23 24; 25 26 27 28 29; 30 31 32 33 34]
T = cell2table(num2cell(B), 'VariableNames', {'ab', 'bc', 'cd', 'de', 'ef'})
T.fg(1:5) = sum(T{3,:}) - T.ab(1)
T.cd(1:3) = inf - inf
Sulaymon Eshkabilov
2022년 7월 19일
This way of an array to table conversion is also feasible and less computationally intensive.
B=[10 11 12 13 14; 15 16 17 18 19; 20 21 22 23 24; 25 26 27 28 29; 30 31 32 33 34]
T = array2table(B, 'VariableNames', {'ab', 'bc', 'cd', 'de', 'ef'})
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!