Matlab-如何根据其他变量命名新变量?价值观?
我想使用函数中给定的其他变量的值来命名变量。 因此,如果我有 x1,x2 的值,我可以将新变量的名称设置为:
x_(x1 的值)_(x2 的值) 作为名称。
我已经检查了 eval、num2str、strcat 函数,但到目前为止我还无法做到这一点,以便我有一个具有上面名称的变量,我可以为其赋值。
任何帮助将不胜感激。
Possible Duplicates:
How to concatenate a number to a variable name in MATLAB?
MATLAB: How can I use a variables value in another variables name?
I want to name a variable using values of other variables given in a function.
So, if I have values for an x1,x2 I can make the new variable's name as:
x_(x1's value)_(x2's value) as a name.
I've checked out the eval, num2str, strcat functions, but as of yet I can't make it so that I have a variable with the name above which I can assign a value to.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看以下常见问题解答:
它回答了问题的“如何”部分,并推荐了一种使用数组的更好方法。
Take a look at the following FAQ:
It answers the "how" part of your question and recommends a better approach using arrays.
正如 Jonas 建议,如果
x1
和x2
是数字,则 此方法有效:
x1
和x2
是字符串,这变成:或更简单(使用串联而不是 SPRINTF):
我不知道你想要完成什么,但这可能这不是最好的方法。 应始终避免使用 EVAL。 在使用 EVAL 时创建变量是(又名“噗噗”)是双重糟糕的。
如果您尝试将参数与值关联起来,结构是一个更好的解决方案:
As Jonas suggests, if
x1
andx2
are numbers this works:If
x1
andx2
are strings, this becomes:or more simply (using concatenation instead of SPRINTF):
I don't know what you're trying to accomplish, but this probably isn't the best way to go about it. EVAL should always be avoided. Creating variables in the using EVAL is (a.k.a. "poofing") is doubly bad.
If you're trying to associate parameters with values, structures are a much better solution:
假设您有充分的理由这样做(并且假设 x1 和 x2 具有整数值),您可以通过组合 EVAL 和 SPRINTF。
如果 x1 和 x2 是浮点数,则会比较棘手,因为变量名称中不能包含点,尽管只要用其他内容替换点仍然是可能的。
不过,我真的要问:你确定要这么做吗?因为目前我无法想象一个应用程序想要创建您事先不知道的变量名称,这反过来又使得编写高效的程序变得非常困难。
编辑
有许多有用的方法可以将数据存储在数组中。如果您确实不想这样做,您可能有兴趣通过 MAP,这是最新版本的 Matlab 中提供的功能。因此,您的密钥将变为
sprintf('%i_%i',x1,x2)
,相应的值将是您想要存储的任何值。Assuming you have a really good reason why you'd want to do that (and assuming x1 and x2 have integer values), you can do this by combining EVAL and SPRINTF.
If x1 and x2 are floats, it'll be trickier since a variable name cannot have dots in it, though it would still be possible as long as you replace the dots with something else.
However, I really have to ask: Are you sure that you want to do that? Because at the moment I cannot imagine an application where would want to create variable names you don't know beforehand, which in turn makes it very hard to write an efficient program.
EDIT
There are many useful ways to store your data in arrays. If you really don't want that, you may be interested in accessing data via key/value pairs in a MAP, a feature which is available in more recent versions of Matlab. Thus, your key would become
sprintf('%i_%i',x1,x2)
, and the corresponding value would be whatever it is you want to store.您还可以使用动态字段引用。 Mathworks 的 Loren 在这里写了一篇文章:
Mathworks:使用-动态字段引用
You can also use dynamic field references. Loren at the Mathworks gives a writeup here:
Mathworks: use-dynamic-field-references