How can I extract values from a histogram (figure)?

334 views (last 30 days)
Hi everyone,
Since I am very new to MatLab and will be needing it for my analysis in the coming days, I was hoping that someone could help me out.
I have a histogram (figure format), but I don't know how to extract the y-values (probability) of each bar and put it either into a list / excel file.
Could someone help me out here?
Thank you!
  5 Comments
Ola Zurek
Ola Zurek on 19 Apr 2019
The histogram was generated by the given script and the output was the histogram in (.fig) file (attached).
Adam Danz
Adam Danz on 19 Apr 2019
Edited: Adam Danz on 19 Apr 2019
If you have access to the script and the input data, you can (and should) get the histogram values from the script rather than from the figure.

Sign in to comment.

Answers (2)

Adam Danz
Adam Danz on 19 Apr 2019
Edited: Adam Danz on 19 Apr 2019
If you have access to the script/function that produced the histogram and the input data, you should get the histogram values from within the code. Find this line of code:
histogram(..., ...)
If you only have the figure and you need to extract the histogram values, here's how.
axisHandle = gca; %handle to the axis that contains the histogram
histHandle = axisHandle.Children; %handle to the histogram
histData = histHandle.Data; %The first input to histogram()
binEdges = histHandle.BinEdges; %The second input to histogram() (bin edges)
% Reproduce the figure
figure
histogram(histData, binEdges)
% If you're looking for the height of each bar:
barHeight = histHandle.Values;

Steven Lord
Steven Lord on 19 Apr 2019
If you only have access to the figure file, use openfig to open it in a figure window then use findobj to obtain the handle of the histogram. Once you have the handle of the histogram, retrieve its properties like Values as Adam Danz showed.
Create a sample histogram for purposes of demonstrating findobj.
x = randn(1, 1e5);
histogram(x)
Find the histogram in the current figure.
h = findobj(gcf, 'Type', 'histogram');
Retrieve the Data of the histogram and compare it to the data originally used to create it.
data = h.Data;
isequal(data, x) % true
Look at the other properties of the histogram.
disp(h)
  2 Comments
Adam Danz
Adam Danz on 19 Apr 2019
@Ola Zurek, Steven Lord's suggestion to get the object handle using findobj() is safer than my suggestion to access the axes' children since there could be more than 1 object plotted on the axes.
Steven Lord
Steven Lord on 19 Apr 2019
And if you're running code to create the histogram and retrieve the bin counts but you don't actually need or want the figure, instead you can call histcounts which will return just the counts and edges. It might not even require a lot of changes to your code since histcounts accepts most or all of the non-graphics options that histogram accepts.

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!