MATLAB Answers

MOHAMED
1

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

MOHAMED
さんによって質問されました 2013 年 4 月 21 日
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
2013 年 4 月 21 日
In a mathematical sense, "a + -b" is meaningful and well-defined.

サインイン to comment.

2 件の回答

回答者: 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
2013 年 4 月 21 日
+1: This is exactly the job the %+d format has been created for.
MOHAMED
2013 年 4 月 21 日
Appreciate your help,
Thanks.

サインイン to comment.


回答者: 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,'+ -','-')

  1 件のコメント

Jan
2013 年 4 月 21 日
For simple jobs, STRREP is much faster than REGEXPREP.

サインイン to comment.



Translated by