format numbers in txt

2 vues (au cours des 30 derniers jours)
Vadim Tambovtsev
Vadim Tambovtsev le 26 Sep 2016
R2=R;
R2(R2==2) = 0.25;
R2(R2==1) = 0.15;
fid = fopen('porosity.txt','wt');
if fid > 0
fprintf(fid,'%d\n',R2');
fclose(fid);
end
When I write the array to a txt file, the format of the values change. From 0.15 to 1.500000e-01 How to keep the initial format 0.15 ?
Thank you.

Réponse acceptée

Star Strider
Star Strider le 26 Sep 2016
The ‘%d’ format descriptor writes integers, and since ‘0.15’ is not an integer, it defaults to an exponential format.
Try this:
fprintf(fid,'%.2f\n',R2');
The ‘%f’ format descriptor writes either fixed-width or variable-width floating point numbers, depending on what you want. See the documentation on fprintf for details.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by