Hello, dear community!
I try to name table variable with non-Latin character, for example Cyrillic:
>> a = table(zq, 'VariableNames', cellstr('я'));
Error using matlab.internal.tableUtils.makeValidName (line 36)
'я' is not a valid variable name.
Error in setVarNames (line 48)
[newnames,wasMadeValid] = matlab.internal.tableUtils.makeValidName(newnames,exceptionMode); % will warn if mods are made
Error in table (line 305)
t = setVarNames(t,vnames); % error if invalid, duplicate, or empty
If I do the same, but with variable named using Latin characters, then everything works fine. What could cause this error? Is this normal behavior?
Thank you.

 채택된 답변

Walter Roberson
Walter Roberson 2015년 10월 22일

1 개 추천

Normal. The names have the same restrictions as matlab variable names. Latin letter to start, continue with digits or underscore or Latin letters

댓글 수: 6

Andrey Kazak
Andrey Kazak 2015년 10월 26일
Thank you. Is this possible to avoid naming columns in a MATLAB table, so it would be default A, B, C, D, ...?
Walter Roberson
Walter Roberson 2015년 10월 26일
If you do not assign VariableName yourself, the default is 'Var1', 'Var2', 'Var3', and so on. You can assign 'A' 'B' and so on if you want.
Andrey Kazak
Andrey Kazak 2015년 10월 26일
That is right. What I want is to export my table to XLSX where first row contains headers with non-Latin (Cyrillic) characters. I see now that this could require some pretty sophisticated workaround.
Walter Roberson
Walter Roberson 2015년 10월 26일
It appears that you could do it in two phases. One phase would have you xlswrite() a cell array that was the headers you want. The second phase would be to writetable() with WriteVariables set to 0 and a Range set to skip over the first row.
Andrey Kazak
Andrey Kazak 2015년 10월 28일
Thank you, Walter. I made this in the following way:
  1. Created a matrix with the data and added top row of NaN
  2. xlswrite the matrix to a new XLSX file
  3. Created cell array of text headers, which is basically a row of text cells
  4. xlswrite the cell array to the same XLSX file
In the result I get with excel spreadsheet with header line and data.
In summary it seems not very convenient that MATLAB table column names are limited to MATLAB variable naming convention.
Walter Roberson
Walter Roberson 2015년 10월 28일
If using xlswrite is acceptable, then use num2cell() to convert the numeric matrix to a cell array, add the headers to the top of that, and xlswrite() the whole thing out.

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

추가 답변 (1개)

Vy Le
Vy Le 2021년 2월 9일

0 개 추천

Hi, why I write it and they said 'inifity norm of number c' is not a valid table variable name
Thank you.
names = {'inifity norm of condition number c','infinity norm of the residual r1'};
table(c,r1,'VariableNames',names)

댓글 수: 1

Prior to release R2019b table variable names were required to be valid MATLAB identifiers. In particular, that meant no spaces. That limitation was removed in release R2019b.
v = (1:5).';
A = table(v, v.^2, v.^3, 'VariableNames', ["v", "v squared", "v^3"])
A = 5x3 table
v v squared v^3 _ _________ ___ 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125

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

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품

태그

질문:

2015년 10월 22일

댓글:

2021년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by