Cell to Number Array

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.

2 Commenti

Rory Hand
Rory Hand il 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});

Accedi per commentare.

Risposte (2)

Stephen23
Stephen23 il 2 Giu 2017

17 voti

str2double(a)

6 Commenti

Anushi1998
Anushi1998 il 2 Giu 2017
Thanks :-)
Asaf McRock
Asaf McRock il 31 Mar 2021
Thanks, Mr. Stephen Cobeldick.
michal.markun
michal.markun il 17 Gen 2023
however:
str2num(a)
returns:
Error using str2num (line 35)
Input must be a character vector or string scalar.
why these two functions work so much differently?
Stephen23
Stephen23 il 17 Gen 2023
"why these two functions work so much different"
Because they are different functions which are defined to work on different inputs.
michal.markun
michal.markun il 18 Gen 2023
Yes, sure, but given the limitations of the str2num listed in the Help:
"The str2num function does not convert cell arrays or nonscalar string arrays, and is sensitive to spacing around + and - operators. In addition, str2num uses the eval function, which can cause unintended side effects when the input includes a function name. To avoid these issues, use str2double.",
"Space characters, or the lack of them, can be significant.",
"str2num converts character arrays and string scalars only. To convert nonscalar string arrays or cell arrays to numeric arrays, use the str2double function."
given that the Help itself suggests using str2double (!), I don't see the point in keeping the str2num function. From the Help files it seems that special cases in which str2num works while str2double does not is when the argument is whole one string/char representing matrices, such as '1 2; 3 4' (rather than {'1' '2';'3' '4'}) or 'false true true false'. To me these are very much special cases...
Never mind, just my thoughts. I keep learning Matlab and probably just have to get used to some of its particularities...
Steven Lord
Steven Lord il 18 Gen 2023
The help suggests using str2double under certain circumstances. It is not a replacement for all uses of str2num. In addition while both functions were introduced prior to release R2006a if I recall correctly str2num predates str2double. If we were to simply remove str2num it would be a backwards incompatibility that potentially would require users to have to modify 17+ years worth of code.

Accedi per commentare.

KSSV
KSSV il 2 Giu 2017

11 voti

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

2 Commenti

Stephen23
Stephen23 il 2 Giu 2017
Modificato: Stephen23 il 2 Giu 2017
See my answer for a simpler and more robust solution ( str2num calls eval, and so is slow and should be avoided).
Anushi1998
Anushi1998 il 2 Giu 2017
Thanks a lot

Accedi per commentare.

Categorie

Prodotti

Richiesto:

il 2 Giu 2017

Commentato:

il 18 Gen 2023

Community Treasure Hunt

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

Start Hunting!

Translated by