When I try to convert from string to number, I lose precision?
whos test_str
Name Size Bytes Class Attributes
test_str 1x9 18 char
K>> test_str
test_str =
0.067354
K>> str2num(test_str)
ans =
0.0674

 Akzeptierte Antwort

José-Luis
José-Luis am 23 Jun. 2014

1 Stimme

format long
str2num(test_str)

2 Kommentare

Andrew Reibold
Andrew Reibold am 23 Jun. 2014
Thank you Jose-Luis
This worked great. I presume that this changes the format for any future things I type as well?
Does anyone know what is the 'default' format if it is not long?
You could read the documentation:
doc format
Note that this only changes how the number is displayed. Its internal representation is, by default in Matlab, a double.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sara
Sara am 23 Jun. 2014

0 Stimmen

Use
sprintf
so you can control the precision you want.

3 Kommentare

Andrew Reibold
Andrew Reibold am 23 Jun. 2014
Bearbeitet: Andrew Reibold am 23 Jun. 2014
Doesn't this return a string?
I am trying to convert a string to a number/double/float.
Sara
Sara am 23 Jun. 2014
I read it the other way around, sorry.
Yep but that method is not efficient. There should be a way to use the exact number of decimals. What if I am using srt2num to find a folder with the same number I looked for?

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by