Using fprintf, how to print 1.234D+02 instead of 1.234E+02?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Tian
am 5 Jan. 2017
Bearbeitet: Walter Roberson
am 6 Jan. 2017
I'd like to print scientific notations into a file, in the format of FORTRAN. For example, I want to print 1.234D+02 instead of 1.234E+02 into the file. How to realize it? Thank you all~
2 Kommentare
Stephen23
am 5 Jan. 2017
fprintf only supports e or E.
You will have to either replace the characters after writing the file, or use sprintf and replace the characters before writing the file.
Akzeptierte Antwort
José-Luis
am 5 Jan. 2017
Bearbeitet: José-Luis
am 5 Jan. 2017
EDIT
As said before, Stephen's answer is the way to go and I had previously posted an erroneous answer. Second try:
mag = @(x) floor(log10(abs(x)));
val = @(x) x./10.^mag(x);
x = [-25, -0.0001, 0, 0.00025, 1.005, 15, 12345];
leftVal = val(x);
leftVal(isnan(leftVal)) = 0;
rightVal = mag(x);
rightVal(rightVal == -Inf | rightVal == Inf) = 0;
sprintf('%.3fD%+03d\n',[leftVal;rightVal])
4 Kommentare
Walter Roberson
am 6 Jan. 2017
Bearbeitet: Walter Roberson
am 6 Jan. 2017
S = sprintf('%.3e',YourNumber)
S(S=='e') = 'D' ;
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Software Development Tools finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!