MATLAB: Histogram: ‘Greater than’ bin

binedgeshistogramlabels

Simple question but I can't seem to find an answer: How do I add a bin to my histogram for the values outside of the visible range? Plotted automatically, the data below has values ranging from 0-200, the last 5 bins (100-200) have one point each. When I change the bin range to 0-100, those 5 bins are cut off. I want to take the points with values greater than the max(BinEdges) value, and have a bin labelled '>100' (populated dynamically as the bin edges are changed).
h=histogram(grains.area);
h.BinLimits=[0 100];
%Make range labels for bins
label = arrayfun(@(x,y) sprintf('%g - %g',x,y), ...
h.BinEdges(1:end-1), h.BinEdges(2:end), ...
'UniformOutput', false);
%label x axis with bin ranges
set(gca,'XTick',(h.BinEdges(1:end-1)+h.BinEdges(2:end))/2,...
'XTickLabel',label)
% get x position for bin count labels
xLblPos=h.BinEdges(1:end-1)+h.BinWidth/2;
%get y position
lift=max(h.Values)/20;
yLblPos=h.Values+lift;
%label each bar at top
cont=string(h.Values);
text(xLblPos,yLblPos,cont)

Best Answer

Add -Inf as the first element of the edges vector and/or Inf as the last element.
>> x = randn(1, 1e6);
>> h = histogram(x, [-Inf -2:0.25:2 Inf]);
>> tooSmallH = h.Values(1)
>> tooSmallX = nnz(x < -2)
>> tooLargeH = h.Values(end)
>> tooLargeX = nnz(x > 2)
The two variables whose names start with tooSmall should have the same value, as should the two variables whose names start with tooLarge.