Extract very small numbers from a .DAT file with all its decimals and export them to an excel file

3 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-3-26
编辑:Stephen23 2020-3-26
"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.

请先登录,再进行评论。

采纳的回答

Ameer Hamza
Ameer Hamza 2020-3-26
编辑:Ameer Hamza 2020-3-26
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 个评论
Stephen23
Stephen23 2020-3-26
编辑:Stephen23 2020-3-26
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.
Sven Krasel
Sven Krasel 2020-3-26
Hello Stephen, hello Ameer,
@Stephen:
The solution Ameer provided is already satisfying enough for me. Your argumentation is totally right but unfortunately I don't have the time to read too much in detail about this. Thank you anyway for your time and your replies. :-)
@Ameer:
Thank you very much. It's a very simple and straight forward solution.
Cheers,
Sven

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by