MATLAB: Is it possible to directly display statistical properties (like mean, standard deviation etc) on a histogram plot in MATLAB 7.8 (R2009a)

histogrampropertiesstatisticalStatistics and Machine Learning Toolbox

I would like to display statistical properties (like mean, std, kurtosis) on my histogram plot (similar to a legend).

Best Answer

Currently, there is no direct way to display statistical properties to the histogram plot in MATLAB 7.8 (R2009a).
Alternatively, you could use the SPRINTF function and the TEXTBOX annotation to display the statistical properties in the figure. As an example, refer to the code below:
clc;
close all;
delta = .5;
pdf = @(x) normpdf(x);
proppdf = @(x,y) unifpdf(y-x,-delta,delta);
proprnd = @(x) x + rand*2*delta - delta;
nsamples = 15000;
x = mhsample(1,nsamples,'pdf',pdf,'proprnd',proprnd,'symmetric',1);
histfit(x,50) %%%Create the histogram
h = get(gca,'Children');
set(h(2),'FaceColor',[.8 .8 1])
mn = mean(x); %%%Calculate the mean
stdv = std(x); %%%Calculate the standard deviation
k = kurtosis(x); %%%The Kurtosis of the data
%%%Create the labels
mnlabel = sprintf('Mean -- %3.2d', mn);
stdlabel = sprintf('Std Deviation -- %3.2d', stdv);
klabel = sprintf('Kurtosis -- %3.2d', k);
%%%Create the textbox
h = annotation('textbox',[0.58 0.75 0.1 0.1]);
set(h,'String',{mnlabel,stdlabel,klabel});