Convert cell to array in matlab

I have a specific cell sized 400x1. It mainly consists of numbers as a string. I mean when I do

mycell{1} 

it gives the result '1'

So you can see that number 1 is in string form. How can I convert this to a numeric array?

+6
source share
3 answers

Similarly, if size(mycell) is 400x1. ,,

 str2num(cell2mat(mycell)) 

... or like that, if the size is 1x400

 str2num(cell2mat(mycell')) 

However, this will cause problems if any of your lines contains a different number of characters, i.e.

 mycell{1} = '2' mycell{2} = '33' 

If you have such a case,

 str2double(mycell) 

... this seems to be ok as indicated in another answer!

+5
source
 str2double(mycell) 

If you have an array of things that look like doubles:

 >> c = {'1' '2' ; '3' '4'} c = '1' '2' '3' '4' >> str2double(c) ans = 1 2 3 4 >> whos ans Name Size Bytes Class Attributes ans 2x2 32 double 

If you have something that doesn't look like a double, you will get NaN in this cell as a result:

 >> c{2,2} = 'aei' c = '1' '2' '3' 'aei' >> str2double(c) ans = 1 2 3 NaN 
+7
source

You can also try cellfun(@str2num,mycell) if you have an array of doubling cells:

mycell = {'1.56548524'; '1.5265'; '-4.2616' ;'-0.2154' ;'2.15'};

you may try

mat = cellfun(@str2num,mycell)

+3
source

Source: https://habr.com/ru/post/917829/


All Articles