Increasing precision when parsing float values using textread()

Hello,
I am using textread() function to parse text values; I have the following line which works well:
[V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14] = textread(file_1,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f','headerlines',5);
The problem I am facing is that float values in the original text contain 6 digits after decimal point, while textread() returns only 4 digits after the decimal point.
For example:
Original value: -0.785834
Value returned by textread(): -0.7858
Please, suggest how to get the full precision of floats when using textread()
Thanks in advance

1 comentario

Are you sure that the value returned is EXACTLY -0.7858, or is that just what matlab is displaying?

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 13 de Oct. de 2014
Editada: Stephen23 el 13 de Oct. de 2014
I just created a new text file containing only the text '0.1234567890123456789', and named it 'temp.txt'. Then I ran this:
>> A = textread('temp.txt','%f');
>> fprintf('%.20f\n',A)
0.12345678901234568000
This shows that textread has read that number up to the double precision limit (~17 sigfig).
The "problem" is likely to be how MATLAB displays numbers up to four decimal digits, much to the confusion of all MATLAB learners, who wonder where their digits have gone to. Don't worry, those digits are still there! Have a look at format for more information on how to change the displaying of numbers.
Also as Adam mentions, the documentation reccomends using textscan instead of textread.

1 comentario

Thanks, indeed the problem was with formatting when displaying the result!

Iniciar sesión para comentar.

Más respuestas (1)

Adam
Adam el 13 de Oct. de 2014
Editada: Adam el 13 de Oct. de 2014
The help page (R2014b) suggests you should use textscan now instead of textread, but I assume the following works for either while textread still exists.
You can specify the precision you want for your floats by e.g.
%.6f
to specify the number of values after the decimal place.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 13 de Oct. de 2014

Comentada:

el 13 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by