Display values with fprintf

51 ビュー (過去 30 日間)
Andrea Tersigni
Andrea Tersigni 2017 年 2 月 24 日
コメント済み: Massimo Zanetti 2017 年 2 月 25 日
I would like to display "The return is ... and the standard deviation is ..."? Returns are of the form 6.0033e-04 while standard deviations 0.0207 What should I write in formatSpec?
if true
Mean_return = mean(...);
Standard = (...)
formatSpec = 'Return= %.. SD= %.. \n';
fprintf(formatSpec,Mean_return,Standard)
end
  8 件のコメント
Guillaume
Guillaume 2017 年 2 月 24 日
編集済み: Guillaume 2017 年 2 月 24 日
@Andrea,
The fact that you ask questions, get answers that exactly answer these questions as asked and do not acknowledge the answers nor mark then as accepted is also likely to not go down well.
And re-asking the same question after somebody has given you answer is like telling them: I don't care about your answer so I'll go and ask somebody else. So, why do we bother?
Massimo Zanetti
Massimo Zanetti 2017 年 2 月 25 日
Facing reality, (in my opinion) we should come to a conclusion which is user-independent. We suggest people to (1) ask questions the right way, (2) accept the answers, etc. Realistically, we cannot believe they are all going to do that.. I think this is why some high-reputation contributors have moderating tools such as delete questions, accept answers, etc. If properly used, these tools allows us to mitigate for incorrect and unpolite utilization of this forum.
It is clear the guy here didn't get the point, so let's clear-up the history and go on.
Max

サインインしてコメントする。

回答 (2 件)

Massimo Zanetti
Massimo Zanetti 2017 年 2 月 24 日
編集済み: Massimo Zanetti 2017 年 2 月 24 日
Here it is
ret = 0.000065003;
std = 0.0207;
fprintf('The return is %.4e and the standard deviation is %.4f \n',ret,std)
to get
The return is 6.5003e-05 and the standard deviation is 0.0207
read the fprintf help page to get printing options based on the number formats.
  2 件のコメント
Andrea Tersigni
Andrea Tersigni 2017 年 2 月 24 日
I get this error:
if true
Error using fprintf
Function is not defined for 'struct' inputs.
Error in PS (line 29) fprintf('The return is %.4e and the standard deviation is %.4f \n',me,st); % code end
Massimo Zanetti
Massimo Zanetti 2017 年 2 月 24 日
It means that your variable(s) me and/or st are structures instead of numerical values. Check it.

サインインしてコメントする。


Jan
Jan 2017 年 2 月 24 日
編集済み: Jan 2017 年 2 月 24 日
formatSpec = 'The return is %g and the standard deviation is %g'
or to specify the decimals:
formatSpec = 'The return is %.4g and the standard deviation is %.4g'

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by