Get properties of histogram in MATLAB r2012a

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

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

Più risposte (1)

Guillaume
Guillaume il 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 Commento
Bianca Elena Ivanof
Bianca Elena Ivanof il 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...

Accedi per commentare.

Categorie

Scopri di più su Data Distribution Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by