MATLAB: Extract data from a histogram from a certain range on the x-axis

histogramplotting

I am trying extract data from a histogram on a certain range on the x-axis without going in individually and counting the 'binned' data using the data cursor. Is there any way on how to do this?

Best Answer

The easiest way is to store the output of the hist() call, which will be the counts (or the counts and the centers if you have two outputs.) You can then bar() the graph into existence:
[counts, centers] = hist(YourData);
bar(centers, counts, 'hist');
Failing that, if you really need to pull data out of an existing hist(), then:
histpatch = findobj(gca, 'type', 'patch');
edgemat = get(histpatch, 'XData');
countmat = get(histpatch, 'YData');
counts = countmat(2,:);
centers = mean(edgemat(2:3,:));