Can anyone help me clean up this code?

2 views (last 30 days)
Sim
Sim on 2 Nov 2012
The function below is used to plot histograms for any give data. Basically what I wnat is to display the mean, median and std on the left hand of the histogram but by lines/arrows. If the mean and median overlap I would want to separate them somehow but indicate them by lines. I have written something as below which does not work.
function data_plot2(data,bins,name,units)
[x,y]=hist(data,bins);
barh(y,x);
mu = mean(data);
mu1 = median(data);
sigma = std(data(:));
mn = min(data(:));
mx = max(data(:));
if nargin==2
xlabel('Bar Lengths of Histogram Bars')
ylabel('Data Distribution')
title('Histogram')
elseif nargin==3
xlabel(varargin{1})
ylabel('Data Distribution')
title('Histogram')
elseif nargin==4
xlabel(varargin(1))
ylabel(varargin(2))
title('Histogram')
end
hold on
plot(0,mn,'c*')
hold on
plot(0,mu,'sr')
hold on
plot(0,mu1,'vk')
hold on
plot(0,sigma,'dg')
hold on
plot(0,mx,'pm')
hold on
legend('Bar Length','Minimum','Mean','Median','Std. Dev.','Maximum')
if abs(mu-mu1)<=30
text(max(x)*10/100,mu,'Mean','Color','r')
hold on
line([0 max(x)*10/100],[mu mu],'Color','r')
hold on
text(max(x)*10/100,mu1+sigma,'Median','Color','g')
hold on
line([0 max(x)*10/100],[mu1 mu1+sigma],'Color','g')
hold on
else
text(max(x)*10/100,mu,'Mean','Color','r')
hold on
line([0 max(x)*10/100],[mu mu],'Color','r')
hold on
text(max(x)*10/100,mu1,'Median','Color','g')
hold on
line([0 max(x)*10/100],[mu1 mu1],'Color','g')
hold on
end
end
Will be looking forward for some suggestion/advice. Thank you in advance.

Answers (0)

Community Treasure Hunt

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

Start Hunting!