我想将我的部分代码压缩为for循环。代码如下:
dataMixSp1 = [dataSp1{1}; dataSp1{2}; dataSp1{3}; dataSp1{4};];
dataMixSp2 = [dataSp2{1}; dataSp2{2}; dataSp2{3}; dataSp2{4};];
dataMixSp3 = [dataSp3{1}; dataSp3{2}; dataSp3{3}; dataSp3{4};];
dataMixSp4 = [dataSp4{1}; dataSp4{2}; dataSp4{3}; dataSp4{4};];
dataMixSp5 = [dataSp5{1}; dataSp5{2}; dataSp5{3}; dataSp5{4};];
dataMixSp6 = [dataSp6{1}; dataSp6{2}; dataSp6{3}; dataSp6{4};];
dataMixSp7 = [dataSp7{1}; dataSp7{2}; dataSp7{3}; dataSp7{4};];
dataMixSp8 = [dataSp8{1}; dataSp8{2}; dataSp8{3}; dataSp8{4};];
dataMixSp9 = [dataSp9{1}; dataSp9{2}; dataSp9{3}; dataSp9{4};];
dataMixSp10 = [dataSp10{1}; dataSp10{2}; dataSp10{3}; dataSp10{4};];
问题是我不知道如何在for循环内动态创建变量。
我尝试了此方法,但是它不起作用:
a = 'dataMixSp';
for idx = 1:10
[a num2str(idx)] = [['dataSp', num2str(idx), '{1}']; ['dataSp' num2str(idx) '{2}']; ['dataSp' num2str(idx) '{3}']; ['dataSp' num2str(idx) '{4}']; ];
end
答案 0 :(得分:0)
答案 1 :(得分:0)
for idx = 1:10
eval( [ 'dataMixSp' num2str(idx) '= [ dataSp' num2str(idx), '{1}; dataSp' num2str(idx) '{2}; dataSp' num2str(idx) '{3}; dataSp' num2str(idx) '{4} ];' ] )
end
将执行以下代码:
dataMixSp1= [ dataSp1{1}; dataSp1{2}; dataSp1{3}; dataSp1{4} ];
dataMixSp2= [ dataSp2{1}; dataSp2{2}; dataSp2{3}; dataSp2{4} ];
dataMixSp3= [ dataSp3{1}; dataSp3{2}; dataSp3{3}; dataSp3{4} ];
dataMixSp4= [ dataSp4{1}; dataSp4{2}; dataSp4{3}; dataSp4{4} ];
dataMixSp5= [ dataSp5{1}; dataSp5{2}; dataSp5{3}; dataSp5{4} ];
dataMixSp6= [ dataSp6{1}; dataSp6{2}; dataSp6{3}; dataSp6{4} ];
dataMixSp7= [ dataSp7{1}; dataSp7{2}; dataSp7{3}; dataSp7{4} ];
dataMixSp8= [ dataSp8{1}; dataSp8{2}; dataSp8{3}; dataSp8{4} ];
dataMixSp9= [ dataSp9{1}; dataSp9{2}; dataSp9{3}; dataSp9{4} ];
dataMixSp10= [ dataSp10{1}; dataSp10{2}; dataSp10{3}; dataSp10{4} ];
亚诺(Arno)