一种在Matlab中动态创建变量的方法? [英] A way to dynamically create variables in Matlab?
问题描述
我正在研究的情况是使用良好的编码做法(例如...)将我收集的一个大型三维数据数组划分出来,现在我需要将此数组的各层划分为单独的变量以进行单独处理在其他地方,我无法像这样BigData(:,:,n)
调用我的数据.
The case I am working on is dividing a big three-dimensional array of data that I have collected using good coding practises (etc...) and now I need to segment the layers of this array into separate variables for individual processing elsewhere, I can't call my data like this BigData(:,:,n)
.
所以我想创建一个循环,在其中创建像这样的新变量
So I would like to create a loop where I create new variables like so
for i=1:n
createVariable('user_' i) = BigData(:,:,i);
end
如何做到这一点而无需每次都手动编写 n 个新变量?
How do I do this without writing n new variables by hand every time?
user_1 = BigData(:,:,1);
user_2 = BigData(:,:,2);
user_3 = BigData(:,:,3);
.
.
.
推荐答案
您的免责声明听起来很有说服力:-)我也会得到那些反对票.但是,要明确一点:为此使用单独的变量是不好的做法.
Your disclaimer sounds convincing :-) I'll get those downvotes too. But, to be clear: using separate variables for this is bad practice.
您可以使用 assignin
来创建变量并为其分配值:
You can use assignin
to create the variables and assign them values:
for ii = 1:n
assignin('base', ['user_' num2str(ii)], BigData(:,:,ii));
end
是的,为此使用单独的变量是不好的做法.
And yes, using separate variables for this is bad practice.
这篇关于一种在Matlab中动态创建变量的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!