array2table won't accept variable name input

조회 수: 10 (최근 30일)
Bob Thompson
Bob Thompson 2021년 3월 24일
댓글: Bob Thompson 2021년 3월 24일
I am attempting to turn an array into a table before I output it into a file, and I am running into an error when I attempt to try and put variable names into my table.
data = randi(100,10,5);
headers = {'T','1','2','3','4'};
tabl = array2table(data,'VariableNames',headers);
Error using array2table (line 62)
'1' is not a valid variable name.
Error in TestCode (line 3)
tabl = array2table(data,'VariableNames',headers);
I don't understand why '1' is not a valid variable name. Can I not use numbers, even when they're a string? I have confirmed the class is 'char' for the actual contents of the headers cell, but it still doesn't seem to like it.

채택된 답변

Jan
Jan 2021년 3월 24일
Which Matlab version are you using? In the current version 2021a I find:
doc table
% Variable names can have any Unicode® characters,
% including spaces and non-ASCII characters.
Your code run without problems.
In former Matlab versions the variables of tables needed to be valid Matlab symbols: < 64 characters, ASCII, no spaces, initial character cannot be an underscore or digit.
  댓글 수: 1
Bob Thompson
Bob Thompson 2021년 3월 24일
I'm using 2016b, so it has to start with a letter, and can't contain spaces.

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

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2021년 3월 24일
편집: Sean de Wolski 2021년 3월 24일
Support for invalid variable names was added recently so you're probably on an older release that required >>isvarname(varname) to be true.
You can
  1. upgrade
  2. call: matlab.lang.makeValidName to turn your names valie
  3. Change the names to valid yourself "One", "Two" would be valid...
  댓글 수: 1
Bob Thompson
Bob Thompson 2021년 3월 24일
Unfortunately, I'm not capable of upgrading.
I'm also not sure if I am capable of modifying the validity of names, and would prefer to avoid that option.
So, in the end I did end up changing the names by adding an alphabetical character at the beginning of each number.

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

카테고리

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

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by