How can I get fprintf to show plus sign in front of positive numbers ?

조회 수: 58 (최근 30일)
Hi everyone, How can I get fprintf to show number with its positive sign ?
It shows only negative signs when the number is negative,
Here is the command line that I used:
fprintf('%4.2f + %4.2fX1 + %4.2fX2 + %4.2fX1^2 + %4.2fX1X2 + %4.2fX2^2 \n \n',a1 ,a2 ,a3 ,a4 ,a5 ,a6)
And here is what I got:
15.10 + 5.67X1 + -2.78X2 + 0.71X1^2 + -0.03X1X2 + -0.76X2^2
as you see, I got "+ - number" which is not meaningful.
Looking for your help.
Regards,
Mohamed Selim
  댓글 수: 1
Jan
Jan 2013년 4월 21일
In a mathematical sense, "a + -b" is meaningful and well-defined.

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

채택된 답변

Daniel Shub
Daniel Shub 2013년 4월 21일
So close ... You want %+4.2f
fprintf('%+4.2f%+4.2fX1%+4.2fX2%+4.2fX1^2%+4.2fX1X2%+4.2fX2^2\n\n',a1 ,a2 ,a3 ,a4 ,a5 ,a6)
  댓글 수: 2
Jan
Jan 2013년 4월 21일
+1: This is exactly the job the %+d format has been created for.
MOHAMED
MOHAMED 2013년 4월 21일
Appreciate your help,
Thanks.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 4월 21일
편집: Azzi Abdelmalek 2013년 4월 21일
s=sprintf('%4.2f + %4.2fX1 + %4.2fX2 + %4.2fX1^2 + %4.2fX1X2 + %4.2fX2^2 \n \n',a1 ,a2 ,a3 ,a4 ,a5 ,a6)
regexprep(s,'+ -','-')

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by