フィルターのクリア

How to reduce decimal point in .dat file?

2 ビュー (過去 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 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by