I have numbers such as 0.1032E-02 (say) If I read this as x = 0.1032E-02 and save it to a text file using fopen, and fprintf fid = fopen('Testing.txt','wt'); fprintf(fid, '%15.5E\n',x) it always prints x as 1.032E-03 but I would like the 0 in front just as the way I assigned the value to x. How do i do it?

댓글 수: 2

Stephen23
Stephen23 2018년 3월 8일
편집: Stephen23 2018년 3월 8일
@Pappu Murthy: Numeric data classes do not store any formatting, so printing a number "just as the way I assigned the value to x" is not possible in general: a value will simply be displayed according to the fprintf specification. With that in mind: if you need to convert to numeric, do you always want a leading zero in the printed output?
Pappu Murthy
Pappu Murthy 2018년 3월 8일
Yes. I want the numbers printed as 0.0000E-nn format with leading zero. But fprintf does not let the leading zero.

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

 채택된 답변

Star Strider
Star Strider 2018년 3월 8일

0 개 추천

Try this little utility anonymous function I wrote a while back to solve this exact problem:
a = 0.1032E-02
expstr = @(x) [x*10.^floor(-log10(abs(x))) floor(log10(abs(x))+1)];
Result = sprintf('%.7fE%+03d', expstr(a))
Result =
'0.1032000E-02'

댓글 수: 5

Pappu Murthy
Pappu Murthy 2018년 3월 8일
This seemed to have worked fine. However, I still have a problem. In order to make the question easy to understand, I over simplified it. I am actually calculating internally the numbers (not reading it like I showed here). Once calculated I need to print them to a file where I need that leading zero.. Not sure your little script can do that. Otherwise you have answered my question for sure..
Jan
Jan 2018년 3월 8일
@Pappu: It does not matter where the numbers are coming from. You can replace the sprintf by fprintf also to write the output to a file.
Star Strider
Star Strider 2018년 3월 8일
@Jan — Thank you!
Pappu Murthy
Pappu Murthy 2018년 3월 8일
It worked like charm. I am going to use this now. Thanks for all the help.
Star Strider
Star Strider 2018년 3월 8일
As always, my (our) pleasure!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

2018년 3월 8일

댓글:

2018년 3월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by