如何在MATLAB中动态创建变量 [英] How to Dynamically Create Variables in MATLAB
问题描述
我想操纵变量名. (就像在PHP中一样,您使用${}
创建动态变量).例如:
I would like to manipulate the variables names. (Just like in PHP you use ${}
to create dynamic variables). For exemple:
我想用A_N作为名称创建N个变量:
I want to create N variables with A_N as their name:
for i = 1:N
A_i = 'new variable!';
end
结果将是:
A_1
A_2
...
A_N
推荐答案
我不了解动态变量,但是通常接受的无结构动态内容存储方式是使用单元格而不是新的变量名称.您可以在单元格的每个元素中存储任何类型.您甚至可以在单元格中存储单元格!您将使用下面的代码.
I don't know about dynamic variables, but the generally accepted way to store dynamic content without structure is to use cells instead of new variable names. You can store any type within each element of the cell,. You can even store cells within cells! You would use the code below.
A = cell(i,1);
for i = 1:N
A{i} = 'new variable!';
end
然后您可以使用以下命令访问动态变量":
Then you can access the "dynamic variable" using:
A{1}
如果您正在寻找更复杂的结构,则也可以考虑结构.要澄清的是,这不是@Tony描述方式的struct方法,而是内置的结构化数组.
If you're looking for a more complex structure, you could consider structs as well. To clarify, this is not the struct method of how @Tony described but as a built in structured array.
这篇关于如何在MATLAB中动态创建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!