How to reduce decimal point in .dat file?

9 views (last 30 days)
I use the following code:
format long g
save 'A.dat' A -ascii
output in .dat file:
9.0000000e+00 2.0000000e+00 4.0000000e+00 7.0000000e+00 1.0000000e+00 8.0000000e+00 5.0000000e+00 1.0000000e+01
However, it should be:
9 2 4 7 1 8 5 10
9.00 2.00 4.00 7.00 1.00 8.00 5.00 10.00
Please let me know how I can solve the problem.

Accepted Answer

Les Beckham
Les Beckham on 11 Aug 2020
For your two alternative ways of saving this data into a text file, try the following two options. Please read the documentation for fprintf which gives you full control over how your data is written to the file. save is not a very good way to save data to a text file, especially if you care about the format of the resulting file.
Option 1:
fp = fopen('A.dat', 'wt');
fprintf(fp, '%3d', A)
Option 2:
fp = fopen('A.dat', 'wt');
fprintf(fp, '%6.2f', A)
It would help to read the documentation for fopen as well.

More Answers (0)


Find more on Large Files and Big Data 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!