Search code examples
arraysmatlabmatrixcell

Matlab: how to trasform a cell array in a matrix


Problem: I have a cell array and every cell of this array has an other cell that contains a string: I want to trasform the cell array in a matrix of strings. How can i do? I have tried to use cell2mat but matlab gives an error: 'Cannot support cell arrays containing cell arrays or objects.'


Solution

  • Suppose you have a row cell array of N cell arrays of P strings:

    C = {{'abc', 'de', 'f'}, {'g', 'hi', 'jkl'}};
    

    then you have 2 ways of organizing the result:

    R1 = vertcat(C{:});
    R2 = horzcat(C{:});
    

    When the array is jagged (different lengths of the inner cell arrays of strings) only one of the options above will work. If the singleton dimension is different between the cells, none of the above options will work.