How to reduce decimal point in .dat file?
1 view (last 30 days)
Show older comments
I use the following code:
format long g
A=round(randperm(10,8));
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
or
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.
0 Comments
Accepted Answer
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)
fclose(fp)
Option 2:
fp = fopen('A.dat', 'wt');
fprintf(fp, '%6.2f', A)
fclose(fp)
0 Comments
More Answers (0)
See Also
Categories
Find more on Text Files 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!