Search code examples
matlabfor-loopiterationcell

multiple cell array looping


I am wondering folks how can I loop over two different data cell arrays.

More precisely, The first

data1 = {'x','y','z', 'xyz','yxz'};
data2 = {'b','c','a'};

I want a for loop that performs the following operation

iterates on the first element of data2 while iterating over the entire elements of data1

Hope you guys can understand my question and looking forward to your amazing talent

Thank you


Solution

  • data1 = {'x','y','z', 'xyz','yxz'};
    data2 = {'b','c','a'};
    
    for k = data2
        for m = data1
            [k{1} m{1}] % Print or use them
        end
    end
    

    where k and m are 1x1 cell arrays, and you can access the string inside them with k{1} or k{:} (all the elements which is only 1 now).