MATLAB Answers

How to reduce decimal point in .dat file?

조회 수: 4(최근 30일)
SM
SM 2020년 8월 10일
답변: Les Beckham 2020년 8월 11일
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