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

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 Kommentare

Have you generated this figure in Matlab?
Or you just have the figure file only.
Ola Zurek
Ola Zurek am 19 Apr. 2019
This figure was generated in Matlab (i didn't write the script, just loaded a picture when running the program).
It's important to know exactly what you imported. For example, was it a *.fig file, that you loaded into MATLAB via the openfig command?
Ola Zurek
Ola Zurek am 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 am 19 Apr. 2019
Bearbeitet: Adam Danz am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Adam Danz
Adam Danz am 19 Apr. 2019
Bearbeitet: Adam Danz am 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 am 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 Kommentare

Adam Danz
Adam Danz am 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.
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Distribution Plots finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 19 Apr. 2019

Kommentiert:

am 19 Apr. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by