How to reduce decimal point in .dat file?

1 次查看(过去 30 天)
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.

采纳的回答

Les Beckham
Les Beckham 2020-8-11
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)
It would help to read the documentation for fopen as well.

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by