How can I export data from MatLab to a .txt file without loosing precision?

조회 수: 14 (최근 30일)
Hello. I would like to know how can export data from MatLab to a .txt file without loosing precision. I would like to export this data so I can do some computations using a C++ code I have. I've done some research, and I have found out that MatLab's double type (default type) has around 16 decimal digits of precision. Here is the code I've written:
x = rand(4,1);
file = fopen('file.txt', 'w');
fprintf(file, '%.15f\n', x);
fclose(file);
I would like to know if this code is correct or if there is something I can improve. Thank you very much for your help!

채택된 답변

Jan
Jan 2013년 2월 3일
편집: Jan 2013년 2월 3일
Not only Matlab's double type uses about 16 digits, but this is the IEEE754 standard, which is available in all numerical software and usual processors. Besides Walter's suggestion to use '%.16g', which is more suitable e.g. for pi*1e14, the conversion to the decimal number as an ASCII file must reduce the precision. Example:
a = pi * 1e16;
fid = fopen('test.txt', 'w');
fprintf(fid, '%.16g', a);
fclose(fid);
fid = fopen('test.txt', 'r');
b = fscanf(fid, '%g');
fclose(fid);
a - b
>> 4
The conversion from the binary double format to the decimal text format includes a certain loss of precision already. When you want to deliver the values accurately, store them in binary format:
fid = fopen('test.bin', 'w');
fwrite(fid, a, 'double);
fclose(fid);
fid = fopen('test.bin', 'r');
b = fread(fid, 1, 'double);
fclose(fid);
a - b
>> 0
You can read the binary format from the C++ function also. And the import is faster than the conversion from the string.
  댓글 수: 4
Jan
Jan 2013년 2월 3일
@Reynaldo: The reading from C++ is very similar to the reading in Matlab. Look for the documentation of FREAD in C++, e.g. http://www.cplusplus.com/reference/cstdio/fread/ (this first link found by Google, but it seems quite clear). When you write 2GB, the binary format has the advantage of using less memory: 16 digits occupy up to 22 bytes (with 'e123' and a separator character), while the exact binary representation needs 8 bytes only.
Tahariet Sharon
Tahariet Sharon 2020년 4월 21일
How to save to txt without space or commas or tabs between the numbers? I want to generate a 100K digit random number, so I first generate the 100K individual random numbers. I just need the spaces to be removed upon exporting.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 2월 3일
I suggest using a "%.16g" format.
  댓글 수: 2
Reynaldo
Reynaldo 2013년 2월 3일
Thank you very much for your suggestion. I have one other question... if I use "%.40g" MatLab will write the number to file with 40 precision digits. I dont understand how is this possible since MatLab is only supposedly able to handle 16 decimal digit precision when using 64-bits. Does MatLab makes up the numbers after the 16th decimal digit? Thanks!
Jan
Jan 2013년 2월 3일
Again this is an effect of the conversion from binary to decimal: Most binary numbers do not have an exact decimal representation. And then the conversion can be calculated with much more than the the significant digits. See James Tursa's http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by