Create a standard normal probability distribution object.
Generate a data vector `x` using random numbers from
the distribution.

pd = makedist('Normal');
rng default; % for reproducibility
x = random(pd,100,1);

Test the null hypothesis that the data in `x` comes
from a population with a normal distribution.

h = chi2gof(x)

h =
0

The returned value `h = 0` indicates that `chi2gof` does
not reject the null hypothesis at the default 5% significance level.

Create a standard normal probability distribution object.
Generate a data vector `x` using random numbers from
the distribution.

pd = makedist('Normal');
rng default; % for reproducibility
x = random(pd,100,1);

Test the null hypothesis that the data in `x` comes
from a population with a normal distribution at the 1% significance
level.

[h,p] = chi2gof(x,'Alpha',0.01)

h =
0
p =
0.3775

The returned value `h = 0` indicates that `chi2gof` does
not reject the null hypothesis at the 1% significance level.

Navigate to the appropriate folder and load the lightbulb
lifetime sample data.

cd(matlabroot);
cd('help/toolbox/stats/examples');
load lightbulb;

Create a vector from the first column of the data matrix,
which contains the lifetime in hours of the lightbulbs.

x = lightbulb(:,1);

Test the null hypothesis that the data in `x` comes
from a population with a Weibull distribution. Use `fitdist` to
create a probability distribution object with `A` and `B` parameters
estimated from the data.

pd = fitdist(x,'Weibull');
h = chi2gof(x,'CDF',pd)

h =
1

The returned value `h = 1` indicates that `chi2gof` rejects
the null hypothesis at the default 5% significance level.

Create six bins, numbered 0 through 5, to use for data
pooling.

bins = 0:5;

Create a vector containing the observed counts for each
bin and compute the total number of observations.

obsCounts = [6 16 10 12 4 2];
n = sum(obsCounts);

Fit a Poisson probability distribution object to the data
and compute the expected count for each bin. Use the transpose operator `.'` to
transform `bins` and `obsCounts` from
row vectors to column vectors.

pd = fitdist(bins','Poisson','Frequency',obsCounts');
expCounts = n * pdf(pd,bins);

Test the null hypothesis that the data in `obsCounts` comes
from a Poisson distribution with a lambda parameter equal to `lambdaHat`.

[h,p,st] = chi2gof(bins,'Ctrs',bins,...
'Frequency',obsCounts, ...
'Expected',expCounts,...
'NParams',1)

h =
0
p =
0.4654
st =
chi2stat: 2.5550
df: 3
edges: [1x6 double]
O: [6 16 10 12 6]
E: [7.0429 13.8041 13.5280 8.8383 6.0284]

The returned value `h = 0` indicates that `chi2gof` does
not reject the null hypothesis at the default 5% significance level.
The vector `E` contains the expected counts for each
bin under the null hypothesis, and `O` contains the
observed counts for each bin.