Search code examples
stringmatlabcell-array

How to combine a different strings under one single cell array in matlab?


I have data in different cells (mostly strings) and I would like to bring some of the data in one cell and keep the rest of the data as it is.

For instance:

A = {'1' '2' '3' '4' '5'; '6' '7' '8' '9' '10'; '11' '12' '13' '14' '15'};

The desired output should be:

B = {'1' '2' '3 4 5'; '6' '7' '8 9 10'; '11' '12' '13 14 15'};

The numbers must be separated by a space.


Solution

  • Using string and join in 16b makes this a bit easier than using strjoin since join works with the dimensionality of matrices.

    >> A = string({'1' '2' '3' '4' '5'; '6' '7' '8' '9' '10'; '11' '12' '13' '14' '15'});
    >> [A(:,1:2) join(A(:,3:end),2)]
    
    ans = 
    
      3×3 string array
    
        "1"     "2"     "3 4 5"   
        "6"     "7"     "8 9 10"  
        "11"    "12"    "13 14 15"