创建具有字符串名称的变量 [英] Create variables with names from strings
本文介绍了创建具有字符串名称的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我想创建 10 个变量,它们看起来像这样:
Let's assume that I want to create 10 variables which would look like this:
x1 = 1;
x2 = 2;
x3 = 3;
x4 = 4;
.
.
xi = i;
这是我打算做的简化版本.基本上我只想通过以自动方式创建这些变量来保存代码行.是否有可能在 Matlab 中构造变量名?我的示例中的模式是 ["x", num2str(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.
推荐答案
你可以用 eval 但你真的应该不
You can do it with eval but you really should not
eval(['x', num2str(i), ' = ', num2str(i)]); %//Not recommended
不如使用元胞数组:
x{i} = i
这篇关于创建具有字符串名称的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文