How can I retrieve data from an histogram?

Hi Everyone,
I have created a figure using histfit(NUMERIC1,n,'normal') where 'NUMERIC1' is the vector, 'n' the number of histograms i want to plot and 'normal' refers to the normal distribution
Once the figure is plotted I get the normal distribution of the vector 'NUMERIC1'. Now, I want to retrieve the data (so the value of the vector 'NUMERIC1') that are contained in each bar. How can I do that?
Thanks,
Matteo

 採用された回答

Adam
Adam 2015 年 3 月 2 日

3 投票

hHist = histfit( rand(100,1), 3, 'normal' )
hHist(1).YData
will give you the numbers if you extract the values from what that gives you.
hHist should be a vector of two graphics handles, the first is a patch that renders the bar chart (the 2nd is a Line object).
Because it is a patch object you get the YData out in slightly less obvious format than you would for a line plot, but the values you want are obvious from its result.

9 件のコメント

Matteo
Matteo 2015 年 3 月 2 日
I'm not sure you understand my question;
What I want are the exact value on the x-axes. For instance with your script:
hHist = histfit( rand(100,1), 3, 'normal' )
the plot will be a bar chart with 3 bars. My goal is to obtain the x_value inside one bar. Assuming that the x-range of the first bar is 0-0.33 what I want are the exact value generated by the function rand(100,1) included in the range 0-0.33
Thanks
Adam
Adam 2015 年 3 月 2 日
hHist(1).XData
will contain the values you want. Again it is complicated by being a patch, but in my example the final row (in fact either of the final 2 rows) will give you the values at the end of each bar and hHist(1).XData(1,1) will give you the starting x-value.
Matteo
Matteo 2015 年 3 月 2 日
I get it. But when I use
hHist(1).XData Matlab said ''??? Improper index matrix reference.''
Star Strider
Star Strider 2015 年 3 月 2 日
The structure is new with R2014b.
Use the get function for earlier versions (and also R2014b):
xd = get(hHist(1), 'XData');
Adam
Adam 2015 年 3 月 2 日
Ah yes. This is a bit of a problem with Matlab answers after such a significant software upgrade.
Since the forum doesn't force people to include their Matlab version it is difficult to know which syntax to use.
Propagating the old syntax for people who are working in R2014b doesn't seem like a good solution, but otherwise anyone < R2014b just gets an answer that requires a correction!
Star Strider
Star Strider 2015 年 3 月 2 日
I actually prefer the ‘old’ handle graphics. I don’t object to the structure concept, but a lot of the old functionality is gone. I’d like to have it back.
Matteo
Matteo 2015 年 3 月 2 日
Thanks a lot! Now is working:)
Star Strider
Star Strider 2015 年 3 月 2 日
@Matteo — If Adam’s Answer solved your problem, please Accept it.
Matteo
Matteo 2015 年 3 月 2 日
Done!

サインインしてコメントする。

その他の回答 (1 件)

Chuck
Chuck 2017 年 9 月 26 日

5 投票

In newer versions of MATLAB, all you need to do is:
counts = h.Values
This will give you the number of values in each bin. You can change the name after the dot above to any of the following:
'BinCounts', 'BinCountsMode', 'BinEdges', 'BinLimits', 'BinLimitsMode', 'BinMethod', 'BinWidth', 'BusyAction', 'ButtonDownFcn', 'CreateFcn', 'Data', 'DeleteFcn', 'DisplayName', 'DisplayStyle', 'EdgeAlpha', 'EdgeColor', 'FaceAlpha', 'FaceColor', 'HandleVisibility', 'HitTest', 'Interruptible', 'LineStyle', 'LineWidth', 'Normalization', 'NumBins', 'Orientation', 'Parent', 'PickableParts', 'Selected', 'SelectionHighlight', 'Tag', 'UIContextMenu', 'UserData', 'Visible', 'Categories'
For example, to call the bin limits:
h.BinLimits

1 件のコメント

Gonzalo Agudelo
Gonzalo Agudelo 2019 年 4 月 28 日
You were able to aquire the number of values per bin, but could you get the values themselves?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

質問済み:

2015 年 3 月 2 日

コメント済み:

2019 年 4 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by