Extract very small numbers from a .DAT file with all its decimals and export them to an excel file
3 views (last 30 days)
Show older comments
Hello everybody,
I'm very desperate already since I've been browsing through the community for days without finding a solution for my problem.
I want to only import the marked numbers (see attached png) from a .dat file (which I can't upload here). This means I need to get rid of the 16 headlines before the data and the first column of the data. The second column (red framed on png) is supposed to be turned into a cell array containing the numbers with all its decimals and export it to an excel spreadsheet.
The main problem I have is that matlab rounds a number like -3.999999999999999819e-06 to -4.000000...e-06 instead of keeping all its decimals. Find the code I've been using so far in the attached .m file.
I would appreciate a lot if you guys could help me out with the coding. I am not experienced but I need to automize this data extraction.
Thanks a lot!
Cheers,
Sven
1 Comment
Stephen23
on 26 Mar 2020
Edited: Stephen23
on 26 Mar 2020
"The main problem I have is that matlab rounds a number like -3.999999999999999819e-06 to -4.000000...e-06 instead of keeping all its decimals."
In fact when that text value is imported this is its exact value** in MATLAB memory:
>> num = dlmread('test.txt');
>> num2strexact(num)
ans =
-3.9999999999999998189924473035450347424557548947632312774658203125e-6
The main problem is most likely confusion about how numeric values are displayed versus how they are stored in memory.
Accepted Answer
Ameer Hamza
on 26 Mar 2020
Edited: Ameer Hamza
on 26 Mar 2020
This issue is related to the finite precision of how the floating-point numbers are represented in computers. The double type in MATLAB can only hold numbers to 16 decimal places. Anything beyond that is theoretically impossible to store in a double variable accurately. In such a case, you can use variable precision arithmetic. Note that this can be many times slowers as compared to the numeric datatype, but if you want this level of accuracy, then variable precision is the only solution.
file = fopen('filename.dat');
data = textscan(file, '%s %s', 'HeaderLines', 16);
fclose(file);
result = vpa(data{2});
6 Comments
Stephen23
on 26 Mar 2020
Edited: Stephen23
on 26 Mar 2020
Copy-and-paste is NOT a reliable way to echange numeric data if you want to maintain precision. Just like with MATLAB (and almost every other numeric computation application in existence), how Excel displays numeric data in NOT the same as what numeric data is stored (either in memory or in a file). I saved your example value in a .xlsx workbook, and this is what is stored in the XML file itself:
but when I copy-and-paste the value directly from the spreadsheet (which copies something what Excel displays) this is what I got:
"When I first extract this number with matlab..."
You did not explain exactly what "extract" means, but clearly it was not numerically robust.
Trying to "fix" how MATLAB imports/exports floating point numeric data is a red-herring. You time would be much better spent on reading about numeric error propagation and floating point numbers.
"Of course it doesn't make any difference but I just want to keep calculating with the same numbers."
Then you really need to avoid Excel and store your data in a reliable format which does not change the data precision, e.g. a binary format such as .mat or something similar.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!