MATLAB Answers

Abraham
0

how can I convert cell2mat when the values are numbers with different number of figures

Asked by Abraham
on 18 Jul 2013
Latest activity Commented on by Walter Roberson
on 14 Jul 2016

Hi, I am trying to convert a 1828x1cell to a matrix so that I could make calculation with it.

The cell contains str in the form

'1'

'23'

'2'

'14'

When a try: data=cell2mat(d);

I get the following error:

_*Error using cat Dimensions of matrices being concatenated are not consistent.

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

So, the problem is that in some case the number is one figure, like 1 and 2, but in other cases the number is two figures like 23 or 14.

Please, how can I solve this problem or what else can I do so that I can do some calculation with the values inside the cell?

Thanks

  0 Comments

Products

No products are associated with this question.

1 Answer

Answer by kjetil87
on 19 Jul 2013
 Accepted answer

your problem is that the cell contains strings, and '1' and '23' does not have the same length, '23' is actually a 1x2 array of char. Therefor it cannot be placed in a matrix, it would work if u replaced '1' with eg '01'. If you are going to do calculations with these numbers i would recommand taking a look at "str2num" or "str2double".

Perhaps

data=cellfun(@str2num,d); 

would solve your problem ?

  4 Comments

Zahra
on 21 Oct 2014

Thanks, it worked!

Thank you very much! This answer was very helpful!

data = str2double(d);

would work as well if d is a cell array of strings.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!