Convert a string to number

Hi, I want to convert a string into a number however I don't want to use str2num since I loose extra digits (see example below). Can I use some other MATLAB function?
data = 0.0782238;
s = str2num(data); %Don't want this since s = 0.0782 and I'm loosing three extra digits

4 comentarios

Patrik Ek
Patrik Ek el 5 de Feb. de 2014
Are you sure? It could just be matlabs display. Try
format long
Lily
Lily el 5 de Feb. de 2014
ok, so MATLAB will store the value but shortens the display? ;)
Patrik Ek
Patrik Ek el 5 de Feb. de 2014
Exactly. You could try
a=1.1111111;
a
b=2.2222222;
format long;
a+b
Lily
Lily el 6 de Feb. de 2014
nice, thx :)

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 5 de Feb. de 2014

1 voto

str2num() does not lose those extra digits. Instead, you are not displaying the full width of the number. At the MATLAB command line give the command
format long g
and try your code again.

Más respuestas (1)

Wayne King
Wayne King el 5 de Feb. de 2014
Editada: Wayne King el 5 de Feb. de 2014

0 votos

how about just using format long
data = '0.0782238';
data = str2num(data);
format long
data

Categorías

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

Etiquetas

Preguntada:

el 5 de Feb. de 2014

Comentada:

el 6 de Feb. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by