Can anyone help me clean up this code?

2 次查看(过去 30 天)
Sim
Sim 2012-11-2
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.

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Histograms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by