Can anyone help me clean up this code?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Histograms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!