MATLAB Answers

Cell to Number Array

2.225 views (last 30 days)
Anushi1998
Anushi1998 on 2 Jun 2017
Commented: Asaf McRock on 31 Mar 2021 at 7:43
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.
  3 Comments
Stephen Cobeldick
Stephen Cobeldick on 17 Nov 2020
"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});

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 2 Jun 2017
str2double(a)
  2 Comments
Asaf McRock
Asaf McRock on 31 Mar 2021 at 7:43
Thanks, Mr. Stephen Cobeldick.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by