2017-01-23 629 views
0

假设我们有一个1×3台A=table(1,2,3);Matlab的“VariableNames”数值与非数值项不接受混合

,其标题名称应包含numericals和非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使用

enter image description here

回答

3

由于您的错误非常明确规定,您提供的变量名的字符串not valid variable nameswhen 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

您可以使用内置-in genvarname将您的字符串转换为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, '[ \-]', '_'); 
+0

谢谢@Suever!不幸的是,这并没有产生预期的结果(参见上面的快照) – John

+0

@John如果'genvarname'的输出不起作用,那么就自己改变标签,使它们成为你想要它们的东西(它们必须是有效的变量名称)。你可以看到我发布的链接定义了一个有效的名字 – Suever

+0

谢谢@Suever!解决方案(例如“from_1st_5th”)现在可以完成它的工作,但我仍然在寻找上述形式的实际结果 – John