How do I get the Histogram datatip in when trying to get "Update Text Update Fcn..."

조회 수: 1 (최근 30일)
The default datatip when you turn on the datacursor shows Bin values if you have a histogram. I would like to make my own function building upon this, however, when I do "Update Text Update Fcn..." I get the other kind of datatip for other plots that shows x, y, z not bins. How do I get a sample of a datatip for histograms? I searched the internet with not hits.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 8월 6일
Amber - you can try the following. It was tested with the first two examples from hist and so may not be foolproof...but it is a start! :) Just paste this body into that for the Text Update Function.
% get the position data
pos = get(event_obj,'Position');
% start the tip text
output_txt = {['Bin Count: ',num2str(pos(2),4)],''};
% get the xdata to determine the bar stats
xdata = get(event_obj.Target,'XData');
% get the child handles to see how many bars there are per bin (need
% to handle multi-bar per bin differently the single bar per bin case)
childhandles=get(get(event_obj.Target,'Parent'),'Children');
if ~isempty(xdata) && ~isempty(childhandles)
% get the bar width, min and max values
minx = min(xdata(:));
maxx = max(xdata(:));
diff = xdata(1,2)-xdata(1,1);
numchildren = length(childhandles);
lowbnd = 0;
uppbnd = 0;
binctr = 0;
if numchildren==1
% only one bar per bin, so easy to determine the lower and upper bounds for
% the bar
lowbnd = pos(1)-diff/2;
uppbnd = pos(1)+diff/2;
binctr = pos(1);
else
% else more than one bar per bin
% note that left-most bar corresponds to the last child, and
% right-most bar corresponds to the first child
% get the far edges of the last bar in each set of numchildren bars
faredges = get(childhandles(1),'XData');faredges=faredges(3,:);
% get the near edges of the first bar in each set of numchildren
% bars
nearedges = get(childhandles(end),'XData');nearedges=nearedges(1,:);
% determine the centres for each group of numchildren bars
barcentres = (faredges+nearedges)/2;
% determine the intervals for each bar centre
if length(barcentres)>1
barintervals = [-Inf (barcentres(2:end)+barcentres(1:end-1))/2 Inf];
elseif length(barcentres)==1
barintervals = [-Inf,Inf];
end
% find the lower and upper bounds for the data
ubidx = find(barintervals>=pos(1),1);
lbidx = ubidx-1;
lowbnd = barintervals(lbidx);
uppbnd = barintervals(ubidx);
binctr = barcentres(lbidx);
end
if lowbnd<=minx
lowbnd = -Inf;
end
if uppbnd>=maxx
uppbnd = Inf;
end
% finish the tip text
output_txt = [output_txt {['Bin Center: ', num2str(binctr,4)], ...
['Bin Edges: [', num2str(lowbnd,4), ...
', ', num2str(uppbnd,4),']']}];
end
Try the above and see what happens!

추가 답변 (1개)

Amber
Amber 2014년 8월 6일
That is exactly what I was looking for. Works great! Thanks.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by