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

4 views (last 30 days)
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.

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Aug 2014
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!

More Answers (1)

Amber
Amber on 6 Aug 2014
That is exactly what I was looking for. Works great! Thanks.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!