3

So far I have this;

Disp_X = X - mean(X);
hist(Disp_X);
h = hist(Disp_X); 

h.BinWidth = 0.001; 

the h.BinWidth was working before to make my bars much narrower, but now I am getting this error message;

"Field assignment to a non-structure array object."

I don't really understand why that wasn't happening before, and now it is.

I am also having a bit of an issue trying to plot two histograms onto one plot. I have frequency of distance from mean in both x and y, and would like them to be plotted on the same graph.

Luka Vlaskalic
  • 445
  • 1
  • 3
  • 19

3 Answers3

4

The command you want is histogram not hist. hist is the old histogram that plots the output. The outputs of that function are the counts and centers of the bins. The output of histogram is a histogram object.

X = rand(1000,1);
Disp_X = X - mean(X);
h = histogram(Disp_X);
h.BinWidth = 0.001;

Then to add another histogram.

hold on
Y = rand(1000,1);
Dy = Y - mean(Y);
h2 = histogram(Dy);
Matt
  • 2,554
  • 2
  • 24
  • 45
2

Matt's answer is correct if you are using R2014b or later, when histogram was introduced. If you are using an older version, then you can't use the dot notation and have to use set:

Disp_X = X - mean(X);
hist(Disp_X);
h = hist(Disp_X); 

set(h, 'BinWidth', 0.001); 
craigim
  • 3,884
  • 1
  • 22
  • 42
2

Both other answers are correct, but the simplest option will be to set this property while calling the function:

histogram(Disp_X,'BinWidth',0.001)
EBH
  • 10,350
  • 3
  • 34
  • 59