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에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by