# Cell to Number Array

2,230 views (last 30 days)
Anushi1998 on 2 Jun 2017
Commented: Stephen Cobeldick on 17 Nov 2020 at 18:41
Hey,
I have created a Graph and I have extracted its edges source node as G.Edges.EndNodes(:,1) and the result is like that
a=[G.Edges.EndNodes(:,1)]
a =
7×1 cell array
'18'
'26'
'33'
'52'
'80'
'86'
'110
Now I have a reference file where I have strings that point to these nodes Names so I want it to extract like
Names(18 26 33 52 80 86 110);
But I am unable to convert Cell array to number array . I tried converting cell array to Table & extracted table column 1 but the result is again a cell array since values in Table are of String Type.Also cell2mat command was not working directly.
So if there is some good way to automate this process since the actual data is large enough to manually handle it.

Stephen Cobeldick on 2 Jun 2017
Do not use the accepted answer if you want fast and robust code. The accepted answer uses cellfun (slow) and str2num (slow and relies on insecure eval).
See my answer for the simplest, neatest, and most efficient solution.
Rory Hand on 17 Nov 2020 at 9:40
Stephen Cobeldick on 17 Nov 2020 at 18:41
"Isn't cell2mat a good alternative?"
Lets try it and find out:
a = {'18';'26';'33';'52';'80';'86';'110'};
cell2mat(a)
Error using cat
Dimensions of arrays being concatenated are not consistent.

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

KSSV on 2 Jun 2017
a = { '18'
'26'
'33'
'52'
'80'
'86'
'110' } ;
iwant = cellfun(@str2num,a)

Stephen Cobeldick on 2 Jun 2017
See my answer for a simpler and more robust solution ( str2num calls eval, and so is slow and should be avoided).
Anushi1998 on 2 Jun 2017
Thanks a lot

Stephen Cobeldick on 2 Jun 2017
str2double(a)

#### 1 Comment

Anushi1998 on 2 Jun 2017
Thanks :-)