Get properties of histogram in MATLAB r2012a

4 views (last 30 days)
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?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 12 Apr 2016
x= [1 2 3 4 5];
nbins = 20;
hist(x,nbins)
set(gcf)
set(gca)

More Answers (1)

Guillaume
Guillaume on 12 Apr 2016
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 Comment
Bianca Elena Ivanof
Bianca Elena Ivanof on 12 Apr 2016
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...

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!