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

13 views (last 30 days)
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!

Accepted Answer

Jan
Jan on 3 Feb 2013
Edited: Jan on 3 Feb 2013
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 Comments
Jan
Jan on 3 Feb 2013
@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 on 21 Apr 2020
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.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 3 Feb 2013
I suggest using a "%.16g" format.
  2 Comments
Reynaldo
Reynaldo on 3 Feb 2013
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 on 3 Feb 2013
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!