How to read and save matrix in full precision
15 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
the cyclist
2013-3-11
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)
0 个评论
Jan
2013-3-11
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!