How to check and convert a column of strings to numbers

27 visualizaciones (últimos 30 días)
Leon
Leon el 8 de Abr. de 2020
Comentada: Leon el 8 de Abr. de 2020
I have some column data like this:
A =
"3"
"3"
"31"
"31"
"57"
"58"
or this
A =
"A05"
"C100"
"G5"
"H33"
Would you please help me with two questions?
(1) How do I check if A can be converted to numers (1 to the first A and 0 to the 2nd A)?
(2) Once I know that, how do I convert them to numerical values?
Thanks.

Respuesta aceptada

Tommy
Tommy el 8 de Abr. de 2020
(2) Convert any string to a double using str2double. "If str2double cannot convert text to a number, then it returns a NaN value."
D = str2double(A);
(1) Check if any element in the result is NaN:
all(~isnan(D))

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by