convert char to double

var = '0' (this is char)
and i want this value convert 'double'
so i used
out = str2double(regexp(var,'\d*','match')
But I have a parse error
How can I get the val value in double?
For reference, if str2double is used, the NAN value is obtained.

1 comentario

Stephen23
Stephen23 el 27 de Sept. de 2022
Editada: Stephen23 el 27 de Sept. de 2022
var = '0'; % v missing parenthesis
out = str2double(regexp(var,'\d*','match'))
out = 0
out = str2double(var)
out = 0

Iniciar sesión para comentar.

Respuestas (1)

Davide Masiello
Davide Masiello el 27 de Sept. de 2022
Editada: Davide Masiello el 27 de Sept. de 2022
var = '0';
out = str2double(var)
out = 0
whos out
Name Size Bytes Class Attributes out 1x1 8 double
No need to use regular expressions at all, at least in Matlab.

3 comentarios

lovedive0142
lovedive0142 el 27 de Sept. de 2022
If I do this, I get a nan value, not a zero. I don't think it's properly converted to double .....
Davide Masiello
Davide Masiello el 27 de Sept. de 2022
What you see in my answer is obtained running Matlab R2022b.
What version do you use?
lovedive0142
lovedive0142 el 27 de Sept. de 2022
i'm using Matlab R2022b too..
Actually, I'm getting this data from another place, but isn't the char value correct if it pops up like this?......

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 27 de Sept. de 2022

Editada:

el 27 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by