How can I export data from MatLab to a .txt file without loosing precision?
Show older comments
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
More Answers (1)
Walter Roberson
on 3 Feb 2013
1 vote
I suggest using a "%.16g" format.
2 Comments
Reynaldo
on 3 Feb 2013
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.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!