Using fprintf, how to print 1.234D+02 instead of 1.234E+02?

조회 수: 4 (최근 30일)
Tian
Tian 2017년 1월 5일
편집: Walter Roberson 2017년 1월 6일
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
Stephen23
Stephen23 2017년 1월 5일
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.
Tian
Tian 2017년 1월 6일
Thank you~ Trying sprintf

댓글을 달려면 로그인하십시오.

채택된 답변

José-Luis
José-Luis 2017년 1월 5일
편집: José-Luis 2017년 1월 5일
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
Tian
Tian 2017년 1월 6일
Thank you for your advice, I want to print numbers
Walter Roberson
Walter Roberson 2017년 1월 6일
편집: Walter Roberson 2017년 1월 6일
S = sprintf('%.3e',YourNumber)
S(S=='e') = 'D' ;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by