Matlab 的 'VariableNames' 不接受数字和 non-numerical 条目的混合
Matlab's 'VariableNames' not accepting mix of numerical & non-numerical entries
假设我们有一个 1x3 table A=table(1,2,3);
其 header 名称应包含数字和 non-numericals:
A.Properties.VariableNames={'from 1st-5th' 'from 6th-10th' ... 'from 11th-15th'};
并生成以下错误:
from 1st-5th' is not a valid variable name.
我试过sprintf
函数来解决这个错误,但是formatSpec
参数让人迷惑。此外,我阅读了有关 eval
的文章,想知道它对我的情况是否有帮助。
genvarname
使用的结果:
正如您的错误非常清楚地指出的那样,您为变量名称提供的字符串是 not valid variable names when they have to be
Variable names, specified as a cell array of character vectors that are nonempty and distinct. Variable names must be valid MATLAB® variable names
您可以使用 built-in genvarname
to convert your strings to valid variable names
A.Properties.VariableNames = genvarname({'from 1st-5th' 'from 6th-10th' 'from 11th-15th'});
或者,提出您自己的有效变量名(无空格或连字符)。
names = {'from 1st-5th' 'from 6th-10th' 'from 11th-15th'};
A.Properties.VariableNames = regexprep(names, '[ \-]', '_');
假设我们有一个 1x3 table A=table(1,2,3);
其 header 名称应包含数字和 non-numericals:
A.Properties.VariableNames={'from 1st-5th' 'from 6th-10th' ... 'from 11th-15th'};
并生成以下错误:
from 1st-5th' is not a valid variable name.
我试过sprintf
函数来解决这个错误,但是formatSpec
参数让人迷惑。此外,我阅读了有关 eval
的文章,想知道它对我的情况是否有帮助。
genvarname
使用的结果:
正如您的错误非常清楚地指出的那样,您为变量名称提供的字符串是 not valid variable names when they have to be
Variable names, specified as a cell array of character vectors that are nonempty and distinct. Variable names must be valid MATLAB® variable names
您可以使用 built-in genvarname
to convert your strings to valid variable names
A.Properties.VariableNames = genvarname({'from 1st-5th' 'from 6th-10th' 'from 11th-15th'});
或者,提出您自己的有效变量名(无空格或连字符)。
names = {'from 1st-5th' 'from 6th-10th' 'from 11th-15th'};
A.Properties.VariableNames = regexprep(names, '[ \-]', '_');