Wrap in a legend with num2str
17 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi everyone!
I'm dealing with probability density functions.
In each figure i create i would like to add to legend the mean, the standard deviation and the cov (coefficient of variation). To do that i'm using the command num2str.
My code is:
%%
Excel_Caramagnabridge=load('caramagna.txt');
af_Caramagnabridge=Excel_Caramagnabridge(:,13);
Cover_Caramagna=Excel_Caramagnabridge(:,12);
CaramagnaEdges=3:0.3:12;
[Caramagna_mean, Caramagna_std]=normfit(af_Caramagnabridge);
Caramagna_cov=Caramagna_std/Caramagna_mean;
Caramagna_distr=normpdf(CaramagnaEdges,Caramagna_mean,Caramagna_std);
testCaramagna=chi2gof(Caramagna_distr);
figure; CaramagnaHist=histogram(af_Caramagnabridge,CaramagnaEdges,'Normalization','pdf');
hold on; f1=plot(CaramagnaEdges, Caramagna_distr, 'linewidth',2, 'color','b'); grid on;
legend([f1],['\mu = ' num2str(round(Caramagna_mean,2)) ' \sigma = ' num2str(round(Caramagna_std,2))], 'orientation','vertical',...
'location','northeast');
If i write in this way, the figure will report mean and std in a row. I just would like to wrap for each statistics.
How to do that?
Thanks!
0 Commenti
Risposta accettata
Rik
il 24 Mar 2019
If you use a newline character ( char(10) ), you can wrap the text. Here is some example code:
figure(1),clf(1)
h=plot(rand(1,10));
mu=0.5;sigma=0.1;
legend(h,sprintf('\\mu=%.2f\n\\sigma=%.2f',mu,sigma))
0 Commenti
Più risposte (1)
Kelsey Dodge
il 4 Lug 2021
The above is great advice. I was looking to add two separate variables onto my graphics. The following worked:
First, I converted my variables to string characters. In this case, I was looking at displaying the mean and mode of my data set.
A1 = num2str(mm,3);
A2 = num2str(mo,3);
Then I called those variables in my legend like this:
legend('Draft (m)', sprintf('Mean = %s', A1), sprintf('Mode = %s',A2))
Which gave me the following:
0 Commenti
Vedere anche
Categorie
Scopri di più su Legend in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!