TYL
TYL

Reputation: 1637

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!

Upvotes: 0

Views: 1825

Answers (2)

Wolfie
Wolfie

Reputation: 30046

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 ); 

Upvotes: 4

A. Syam
A. Syam

Reputation: 849

You may try the below one:

a = {[1.2]; [1.3]; [1.45]}
cell2mat(a)

Upvotes: -1

Related Questions