Search code examples
matlabmatrixcell

Cell2mat for cell arrays with numeric strings of different size in Matlab


Hi I have a cell array which I want to convert to a matrix:

a = {'1.2'; '1.3'; '1.45'}
cell2mat(a)

Gives me the error:

Error using cat
CAT arguments dimensions are not consistent.

Error in cell2mat (line 84)
            m{n} = cat(1,c{:,n});

Please help thank you!


Solution

  • cell2mat fails because it's expecting numeric elements in the cell array, to be placed in a matrix. You have character arrays, not numeric elements, so you need to use str2double to convert them to doubles (the output is a matrix as desired).

    a = {'1.2'; '1.3'; '1.45'};
    out = str2double( a );