how to substitute, in a table, variable names with the values of a vector

조회 수: 1 (최근 30일)
Francesco Grechi
Francesco Grechi 2021년 4월 2일
댓글: Steven Lord 2021년 4월 2일
i have a table of 6 colums U and each colums have its title (Nation, valueA, valueB, valueC, valueD, valueE). I want to substitute the last 5 colums names with the numbers contained in the vector X.
X = [1 2 3 4 5 ]
U is 20x6

답변 (1개)

Sajid Afaque
Sajid Afaque 2021년 4월 2일
for num = 2:5
table_name.Properties.VariableNames(num) = {'New substitute name here in this case X(num-1)'};
end
  댓글 수: 2
Francesco Grechi
Francesco Grechi 2021년 4월 2일
i think that could be a good idea however if i put:
table_name.Properties.VariableNames(num) = {'X(num-1)'};
i got the following error (because matlab doesn't accept the ''):-->
'X(num-1)' is not a valid table variable name. See the documentation for isvarname or matlab.lang.makeValidName for more information.
Steven Lord
Steven Lord 2021년 4월 2일
The ability to specify table variable names that are not valid MATLAB identifiers was introduced in release R2019b. Prior to that all table variable names had to be valid MATLAB identifiers (as per the isvarname function.)
T = array2table(magic(4))
T = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
newVariableNames = {'aardvark', 'baboon', 'camel', 'dog'};
T.Properties.VariableNames = newVariableNames
T = 4×4 table
aardvark baboon camel dog ________ ______ _____ ___ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by