Clear Filters
Clear Filters

How to read and save matrix in full precision

11 views (last 30 days)
Thomas
Thomas on 11 Mar 2013
Dear list,
I have a data file with 245 rows and 19 columns containing numbers with 9-digit deciman precision. Below is the first line:
1.259411696 80.725541123 0.397698672 9.453072484 19.901795901 24932908.192915406 449.088485915 485.063672597 1565.024809328 1577.924419388 10.749349506 0.547323907 49.039758899 861.176885206 0.903715327 7.147523310 44.290673508 34.844124016 50.110785484
When I load the data into a matrix using "texscan", the largest number (6th) dominates and the rest are rounded and saved in the form "%1.4f * 1.0e+07". Is there any way to save the numbers in a matrix as they are in data file without losing precision?
Here's an example of what I mean:
str='1.259411696 80.725541123 0.397698672 9.453072484 19.901795901 24932908.192915406 449.088485915 485.063672597 1565.024809328 1577.924419388 10.749349506 0.547323907 49.039758899 861.176885206 0.903715327 7.147523310 44.290673508 34.844124016 50.110785484';
data = textscan(str, '%.9f')
celldisp(data)
data{1} =
1.0e+07 *
0.0000
0.0000
0.0000
0.0000
0.0000
2.4933
0.0000
0.0000
0.0002
0.0002
0.0000
0.0000
0.0000
0.0001
0.0000
0.0000
0.0000
0.0000
0.0000

Answers (2)

the cyclist
the cyclist on 11 Mar 2013
Values are stored in double precision by default, but only a few digits are displayed by default. You can change the display using the format command.
For example, try
>> format long
>> celldisp(data)

Jan
Jan on 11 Mar 2013
Beside the effect mentioned by the cyclist, the conversion from decimal to binary and backwards loose accuracy. There are binary numbers, which cannot be represented exactlyas decimal numbers and vice-versa. What it the 9-digit representation of 0.9999999999999998?
So if you need to keep a certain accuracy, do not store values as ASCII text, but binary.

Community Treasure Hunt

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

Start Hunting!