string 创建具有字符串名称的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16099398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:53:24  来源:igfitidea点击:

Create variables with names from strings

stringmatlabvariablesvariable-names

提问by Potaito

Let's assume that I want to create 10 variables which would look like this:

假设我想创建 10 个如下所示的变量:

x1 = 1;
x2 = 2;
x3 = 3;
x4 = 4;
.
.
xi = i;

This is a simplified version of what I'm intending to do. Basically I just want so save code lines by creating these variables in an automated way. Is there the possibility to construct a variable name in Matlab? The pattern in my example would be ["x", num2str(i)]. But I cant find a way to create a variable with that name.

这是我打算做的简化版本。基本上我只想通过以自动方式创建这些变量来保存代码行。是否有可能在 Matlab 中构造变量名?我的例子中的模式是["x", num2str(i)]. 但是我找不到创建具有该名称的变量的方法。

回答by Dan

You can do it with evalbut you really should not

你可以做到这一点的eval,但你真的应该

eval(['x', num2str(i), ' = ', num2str(i)]); %//Not recommended

Rather use a cell array:

而是使用元胞数组:

x{i} = i

回答by bdecaf

I also strongly advise using a cell array or a struct for such cases. I think it will even give you some performance boost.

我还强烈建议在这种情况下使用元胞数组或结构体。我认为它甚至会给你一些性能提升。

If you really need to do so Dan told how to. But I would also like to point to the genvarnamefunction. It will make sure your string is a valid variable name.

如果你真的需要这样做,丹告诉了如何去做。但我也想指出这个genvarname功能。它将确保您的字符串是有效的变量名称。

EDIT: genvarname is part of core matlab and not of the statistics toolbox

编辑:genvarname 是核心 matlab 的一部分,而不是统计工具箱的一部分

回答by MasterJedi

for k=1:10
   assignin('base', ['x' num2str(k)], k)
end

回答by eyalsoreq

Although it is long overdue, i justed wanted to add another answer.

虽然早就应该了,但我只是想添加另一个答案。

the function genvarname is exactly for these cases

函数 genvarname 正是针对这些情况

and if you use it with a tmp structure array you do not need the eval cmd

如果您将它与 tmp 结构数组一起使用,则不需要 eval cmd

the example 4 from this link is how to do it http://www.mathworks.co.uk/help/matlab/ref/genvarname.html

此链接中的示例 4 是如何操作http://www.mathworks.co.uk/help/matlab/ref/genvarname.html

 for k = 1:5
   t = clock;
   pause(uint8(rand * 10));
   v = genvarname('time_elapsed', who);
   eval([v ' = etime(clock,t)'])
   end

all the best

祝一切顺利

eyal

艾尔

回答by Potaito

If anyone else is interested, the correct syntax from Dan's answer would be:

如果其他人有兴趣,Dan 的答案中的正确语法是:

eval(['x', num2str(i), ' = ', num2str(i)]);

My question already contained the wrong syntax, so it's my fault.

我的问题已经包含错误的语法,所以这是我的错。

回答by Fabian Jonsson

I needed something like this since you cannot reference structs (or cell arrays I presume) from workspace in Simulink blocks if you want to be able to change them during the simulation.

我需要这样的东西,因为如果您希望能够在模拟期间更改它们,则无法从 Simulink 块中的工作区引用结构(或我假设的元胞数组)。

Anyway, for me this worked best

无论如何,对我来说这最有效

assignin('base',['string' 'parts'],values);