Matlab-如何根据其他变量命名新变量?价值观?

发布于 2024-09-26 05:26:05 字数 551 浏览 4 评论 0原文

可能的重复:
如何将数字连接到变量名称MATLAB?
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一张白纸 2024-10-03 05:26:05

查看以下常见问题解答:

它回答了问题的“如何”部分,并推荐了一种使用数组的更好方法。

Take a look at the following FAQ:

It answers the "how" part of your question and recommends a better approach using arrays.

狼亦尘 2024-10-03 05:26:05

正如 Jonas 建议,如果 x1x2 是数字,

x1 = 3;
x2 = 4;
newValue = 25;

eval(sprintf('x_%i_%i = newValue;',x1,x2));

则 此方法有效: x1x2 是字符串,这变成:

x1 = 'foo';
x2 = 'bar';
newValue = 25;

eval(sprintf('x_%s_%s = newValue;',x1,x2));

或更简单(使用串联而不是 SPRINTF):

x1 = 'foo';
x2 = 'bar';
newValue = 25;

eval(['x_' x1 '_' x2 ' = newValue']);

我不知道你想要完成什么,但这可能这不是最好的方法。 应始终避免使用 EVAL。 在使用 EVAL 时创建变量是(又名“噗噗”)是双重糟糕的。

如果您尝试将参数与值关联起来,结构是一个更好的解决方案:

x1 = 'foo';
x2 = 'bar';
newValue = 25;

x.([x1 '_' x2]) = newValue;

As Jonas suggests, if x1 and x2 are numbers this works:

x1 = 3;
x2 = 4;
newValue = 25;

eval(sprintf('x_%i_%i = newValue;',x1,x2));

If x1 and x2 are strings, this becomes:

x1 = 'foo';
x2 = 'bar';
newValue = 25;

eval(sprintf('x_%s_%s = newValue;',x1,x2));

or more simply (using concatenation instead of SPRINTF):

x1 = 'foo';
x2 = 'bar';
newValue = 25;

eval(['x_' x1 '_' x2 ' = newValue']);

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 = 'foo';
x2 = 'bar';
newValue = 25;

x.([x1 '_' x2]) = newValue;
秋日私语 2024-10-03 05:26:05

假设您有充分的理由这样做(并且假设 x1 和 x2 具有整数值),您可以通过组合 EVALSPRINTF

x1 = 3;
x2 = 4;
newValue = 25;
eval(sprintf('x_%i_%i = newValue;',x1,x2));

如果 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.

x1 = 3;
x2 = 4;
newValue = 25;
eval(sprintf('x_%i_%i = newValue;',x1,x2));

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.

故事灯 2024-10-03 05:26:05

您还可以使用动态字段引用。 Mathworks 的 Loren 在这里写了一篇文章:

Mathworks:使用-动态字段引用

You can also use dynamic field references. Loren at the Mathworks gives a writeup here:

Mathworks: use-dynamic-field-references

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文