在Matlab中将变量命名为字符串 [英] Name a variable as string in Matlab
问题描述
假设变量FileName包含一个字符串,例如Name1.如何创建名称为Name1的变量?
Assume the variable FileName contains a string such as Name1. How do I make a variable with the name Name1?
此页面上的示例4似乎很相似,但是我无法使其正常工作.这是正确的方法吗? http://se.mathworks.com/help/matlab/ref/genvarname.html
The example 4 at this page seems to be similar, but I cant get it to work. Is it the right way to do it? http://se.mathworks.com/help/matlab/ref/genvarname.html
推荐答案
对于您要执行的操作,eval
函数可为您提供:
For what you want to do, the eval
function is there for you:
FileName = 'Name1';
eval([FileName ' = 18;']); % Executes "Name1 = 18;"
,现在已创建变量Name1
,其值为18
.
and now the variable Name1
is created and has a value of 18
.
函数genvarname
具有不同的用途,即生成可接受且无冲突的变量名称,而不是变量本身.
The function genvarname
has a different purpose, which is to generate acceptable and non-conflicting variable names, and not the variables themselves.
这篇关于在Matlab中将变量命名为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!