How can I export data from MatLab to a .txt file without loosing precision?
17 次查看(过去 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!
0 个评论
采纳的回答
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
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
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
2013-2-3
I suggest using a "%.16g" format.
2 个评论
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 Center 和 File 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!