How can I extract values from a histogram (figure)?
조회 수: 327 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Adam Danz
2019년 4월 19일
편집: Adam Danz
2019년 4월 19일
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;
For more information about what's stored in 'histHandle': https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.histogram-properties.html
댓글 수: 0
Steven Lord
2019년 4월 19일
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
Adam Danz
2019년 4월 19일
@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
2019년 4월 19일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!