Display values with fprintf

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 commentaires

Guillaume
Guillaume le 24 Fév 2017
See also plot-titles-in-for-loop-with-string-and-variable-names where you were already told (twice) to read the doc.
Adam
Adam le 24 Fév 2017
Modifié(e) : Adam le 24 Fév 2017
Andrea Tersigni
Andrea Tersigni le 24 Fév 2017
I asked four times because even if i read all the documentation I still can't do it and I thought this was the place to get help. If someone needs help and you keep cutting and pasting pieces of documentation then Matlab answers is useless.
Adam
Adam le 24 Fév 2017
You haven't shown in any of the questions that you have made any attempt to use fprintf or sprintf. Matlab Answers isn't a substitute for people being too lazy to read documentation, it is here to augment the documentation when questions are either more complicated or it is unclear, but just saying you've read the documentation and you still don't understand without showing any attempt that you made doesn't generally go down well and asking the same question multiple times definitely doesn't.
Andrea Tersigni
Andrea Tersigni le 24 Fév 2017
I'm working on this from this morning and maybe you're too lazy to read below but I posted the errors i got trying everything i READ on the documentation. If you're too good in programming to help beginners then you shouldn't open questions.
Adam
Adam le 24 Fév 2017
Modifié(e) : Adam le 24 Fév 2017
Reading the documentation is absolutely the best way for a beginner to learn.
You posted errors that you got, but not the full code that produced them. All we can deduce from the error is exactly what the error tells you as well as us. Matlab errors are generally very good at telling you what the problem is.
Your error below says the function is not defined for struct inputs. If you click on the hyperlinks for the inputs of a function in the documentation (in this case the 'A1,...An') they take you to a section that tells you what types the inputs are allowed to be. In this case it says 'Numeric or character arrays' and you apparently gave it a struct, but you didn't post any code that would give us an idea that what you give it is a struct!
Some people will simply give you a ready done answer to your question which you can copy and paste into your code and move on without needing to understand it. I usually prefer to point you to the right place in the documentation and then answer any questions that result from that so that you learn as you do it. Sometimes I am very patient when I do it, sometimes I am less so if someone keeps posting the same questions without taking on board any of the responses!
Guillaume
Guillaume le 24 Fév 2017
Modifié(e) : Guillaume le 24 Fév 2017
@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 le 25 Fév 2017
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

Connectez-vous pour commenter.

Réponses (2)

Massimo Zanetti
Massimo Zanetti le 24 Fév 2017
Modifié(e) : Massimo Zanetti le 24 Fév 2017

1 vote

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 commentaires

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 le 24 Fév 2017
It means that your variable(s) me and/or st are structures instead of numerical values. Check it.

Connectez-vous pour commenter.

Jan
Jan le 24 Fév 2017
Modifié(e) : Jan le 24 Fév 2017

0 votes

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'

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by