Get properties of histogram in MATLAB r2012a

1 回表示 (過去 30 日間)
Bianca Elena Ivanof
Bianca Elena Ivanof 2016 年 4 月 12 日
Hey
I am trying to plot a histogram as follows:
x= [1 2 3 4 5];
nbins = 20;
histogram = hist(x,nbins)
However, what I obtain is histogram = count nbins (as if I used the count function) and not a figure; because of this, if I try to get the properties of the figure using get(histogram) I get an error.
I tried only hist(x,nbins), without naming the histogram as above, and that does give me a figure. But then, when I try to get the properties of the figure using get(hist(x,nbins) I get an error.
What I am trying to achieve is to create a histogram, get its properties and then play around with them using 'set'. For that I need to be able to first name the histogram, I assume.
Could anyone please help me?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 12 日
x= [1 2 3 4 5];
nbins = 20;
hist(x,nbins)
set(gcf)
set(gca)
  1 件のコメント
Bianca Elena Ivanof
Bianca Elena Ivanof 2016 年 4 月 12 日
Brilliant! Thanks!

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

その他の回答 (1 件)

Guillaume
Guillaume 2016 年 4 月 12 日
Matlab replaced hist with histogram in newer versions of matlab partly because of this silly behaviour.
You have to use hist without any output argument for it to create a figure (or reuse an existing one). It uses the current axes in the current figure. So you could just query these afterward with gca and gcf respectively:
hist(x, nbins); %create histogram in current axes of current figure. If none exist, create them
hfig = gcf; %handle to current figure
hax = gca; %handle to current axes
set(hax, 'XTick', 1:5);
  1 件のコメント
Bianca Elena Ivanof
Bianca Elena Ivanof 2016 年 4 月 12 日
Yep, thank you! It all makes sense now. I suspected that there was a minor snag with the 2012 version since on the MathWorks blog they use histogram and not hist...

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

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by