0

I want to compress part of my code into a for loop. The code looks like this:

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};];

The problem is that I don't know how to dynamically create variables inside the for loop.

I tried this but it is not working:

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
Alejandro
  • 31
  • 6
  • 3
    I assume there is absolutely no way that dataMixSp1,2,3,...,10 and dataSp1,2,3...,10 could be in a cell-matrix? Like, dataSp{1} = dataSp1. – tryman Feb 01 '19 at 19:21
  • 3
    In 99% of the times I was facing this problem, a different way to store my data was the better solution. – tryman Feb 01 '19 at 19:28
  • Please read the arguments in the top answer to the linked question, as well as the links provided there. I very, very, very strongly recommend that you do not use variable names like these. Instead, use cell arrays. For example, you could have collected your data as `dataSp{1,2}` rather than `dataSp1{2}`. Then in your loop you can write `dataMixSp{ii} = cat(1,dataSp{ii,:});`. – Cris Luengo Feb 02 '19 at 08:43

2 Answers2

0

To have Matlab evaluate an instruction in a string, use the eval function. For the problem you show, this could be done as:

root = 'dataMix';
part = 'dataSp';
for k=1:10
    target = sprintf('%s%d', root,k); % creates strings 'dataMix1', 'dataMix2', ...
    thisPart = sprintf('%s%d', part,k); %creates strings 'dataSp1, 'dataSp2', ...
    rhs = '['; % the opening of the right-hand-side of the assignment
    for n=1:4
        rhs=sprintf('%s%s{%d};',rhs,thisPart,n);%appends 'dataSp1{1}, dataSp1{2}...
    end
    rhs=strcat(rhs(1:end-1),']');% close the right-hand-side 
    % note the (1:end-1) to remove the ';' between the last variable and the ']'
    instruction = sprintf('%s = %s;',target,rhs); % create a Matlab instuction
    eval(instruction) % have Matlab evaluate the instruction
end

Although, as @tryman said, sometimes the problem is easier to solve saving the data in a different way.

Hope this helps

JAC

JAC
  • 396
  • 3
  • 9
  • 1
    Yes, this is a solution, but `eval` is really, really bad and should not be recommended. – Cris Luengo Feb 02 '19 at 08:32
  • @Cris Luengo: I agree. My solution is really a hack. A better solution would be a different way to store the date as suggested by you and tryman. JAC – JAC Feb 02 '19 at 20:13
  • What would be the correct way to store the data? – Alejandro Feb 04 '19 at 13:54
  • As @Cris Luengo suggested, use `dataMix{1}` instead of `dataMixSp1` and `dataSp{1,1}` instead of `dataSp1{1}` with similar changes for `dataMix2`, `dataMix3`, ..., `dataSp2`, `dataSp3`, ... – JAC Feb 04 '19 at 14:16
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

Will execute the following code:

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

user1097111
  • 662
  • 5
  • 15