# controlchart

Shewhart control charts

## Syntax

## Description

`controlchart(`

creates an X-bar chart of the
measurements in `X`

)`X`

. If `X`

is a matrix, each row is a
subgroup of measurements containing replicate measurements taken at the same time. The rows
of `X`

must be in time order. If `X`

is a
`timeseries`

object, the sample at each time must contain replicate
measurements.

`controlchart(`

creates an X-bar chart of the measurements in `X`

,`group`

)`X`

, with subgroups
specified in `group`

. `group`

is a categorical
variable, numeric vector, character vector, string array, or cell array of character vectors
that has size equal to `size(X,1)`

(or `size(X.data,1)`

,
if `X`

is a `timeseries`

object).. If
`X`

is a matrix or `timeseries`

object, the software
groups the measurements according to the rows or samples of `X`

, and uses
`group`

to label the `x`

-axis of the last chart. If
`X`

is a vector, the software groups consecutive measurements
`X(n)`

that share the same value of `group(n)`

for 1 ≤
`n`

≤ `length(X)`

. Subgroups can have different numbers
of measurements.

`controlchart(___,`

specifies additional options using one or more name-value arguments in addition to any of
the input argument combinations in the previous syntaxes. For example, you can specify the
control limits, the specification limits, and the chart type.`Name=Value`

)

returns
a structure that contains subgroup statistics and parameter estimates.`stats`

= controlchart(___)

## Examples

### X-Bar and R Charts

Load the `parts`

data set and display its size.

```
load parts
size(runout)
```

`ans = `*1×2*
36 4

The matrix `runout`

contains 36 subgroups. Each subgroup contains four replicate measurements of the same quantity.

Create X-bar and R control charts for the data, and return the subgroup statistics.

stats = controlchart(runout,ChartType=["xbar","r"]);

Display the process mean and standard deviation.

stats.mu

ans = -0.0864

stats.sigma

ans = 0.1302

The X-bar chart plots the arithmetic mean of each subgroup. The green center line indicates the mean of all the elements of `runout`

, and the red lines indicate the upper and lower control limits.

The R chart plots the range of each subgroup. The green center line indicates the mean range, averaged over the subgroups.

In both plots, the circled points indicate subgroups that violate the control limits.

### I and MR Charts

Load the `parts`

data set and keep only the first measurement of each subgroup.

```
load parts
X = runout(:,1);
```

Create `"i"`

(individual) and `"mr"`

(moving range) control charts. Use a window width of two measurements to calculate the moving range.

controlchart(X,ChartType=["i","mr"],Width=2)

The first chart plots the individual measurement values in sequential order. The second chart plots the absolute difference between each measurement and the previous measurement. The green center lines represent the mean quantities, and the red lines represent the control limits.

### P and NP Charts

Generate a simulated data set `X`

that consists of pass or fail measurements of 10 units, taken on 40 consecutive days. Each day represents a subgroup, and each unit measurement has a 20% chance of indicating a failure. Represent a failure as a logical `1`

(`true`

) and a pass as a logical `0`

(`false`

).

rng(0,"twister") % For reproducibility failureProbability = 0.2; randomMatrix = rand(40,10); X = logical(randomMatrix < failureProbability);

Create `"p"`

and `"np"`

control charts of the measurements and return the subgroup statistics and plotted point values. Specify a unit size of `1`

to indicate that each element in X is a logical value for a single unit.

[stats,plotted] = controlchart(X,ChartType=["p","np"],Unit=1);

The `"p"`

chart plots the fraction of failure measurements in each subgroup. The `"np"`

chart plots the number of failure measurements in each subgroup.

Display the mean fraction of failure measurements.

stats.p

ans = 0.2200

Display the indices of points that are out of control.

find(plotted(2).ooc)

ans = 16

Subgroup `16`

is marked as a control violation in both charts since it contains 8 failures and exceeds the upper control limits.

### U and C Charts

Generate simulated measurements of the number of defects in five units, taken on 50 consecutive days. Each unit has a size between `1`

and `10`

cm, and the expected number of defects per cm is `0.25`

.

rng(0,"twister") % For reproducibility unitSize = [10,3,5,2.5,8]; % Unit sizes lambda = 0.25; % Expected number of defects per cm nDays = 50; X = []; units = []; for i = 1:nDays defects = poissrnd(lambda*unitSize); X = [X; defects]; units = [units; unitSize]; end

Create a U chart of the measurements and return the subgroup statistics and plot data.

`[stats,Uplot] = controlchart(X,ChartType="u",Unit=units);`

The U chart plots the number of defects per cm for each day, measured over all units. Display the value of the center line $$m$$, which is mean number of measured defects per cm, averaged over all days.

stats.m

ans = 0.2407

The value is close to the expected defect rate of `0.25`

. The red lines represent the upper and lower control limits, which are equal to $$m\pm 3\sqrt{m/S}$$, where $$S$$ is `sum(unitSize)`

. Display the upper control limit value.

Uplot.ucl(1)

ans = 0.5164

Create a `"c"`

chart of the measurements and return the subgroup statistics and plot data.

`[stats,Cplot] = controlchart(X,ChartType="c",Unit=units);`

The `"c"`

chart plots the number of measured defects on each day. Display the value of the center line ${\mu}_{c}$, which is the mean number of measured defects, averaged over all days.

Cplot.cl(1)

ans = 6.8600

Display the value of the upper control limit, which is equal to $${\mu}_{c}+3\sqrt{{\mu}_{c}}$$.

Cplot.ucl(1)

ans = 14.7175

Display the total number of defects and defects per cm values for day 49.

Cplot.pts(49)

ans = 15

Uplot.pts(49)

ans = 0.5263

The software marks day 49 as a violation in both plots, because its total number of defects and defects per cm values exceed the upper control limits.

## Input Arguments

`X`

— Measurements

numeric matrix | numeric vector | `timeseries`

object

Measurements, specified as a numeric matrix, numeric vector, or
`timeseries`

object. If `ChartType`

contains
`"p"`

, `"np"`

, `"u"`

, or
`"c"`

, then the elements of `X`

must be
nonnegative.

If you specify `X`

as a matrix:

Each row of

`X`

is a subgroup of measurements containing replicate measurements taken at the same time.The rows of

`X`

must be in time order.If you specify

`group`

, the software groups the measurements by rows, and uses`group`

to label the`x`

-axis of the last chart.

If you specify `X`

as a vector and
`ChartType`

contains `"xbar"`

,
`"s"`

, or `"r"`

, then you must also specify
`group`

.

If you specify `x`

as a `timeseries`

object, the
sample at each time must contain replicate measurements.

**Data Types: **`single`

| `double`

`group`

— Groups

numeric vector

Groups, specified as a numeric vector that has length equal to
`size(X,1)`

(or `size(X.data,1)`

, if
`X`

is a `timeseries`

object). If
`X`

is a matrix, the software groups the measurements according to
the rows of `X`

, and uses `group`

to label the
`x`

-axis of the last chart. If `X`

is a vector, a
subgroup consists of consecutive measurements `X(n)`

that share the
same value of `group(n)`

for 1 ≤ `n`

≤
`length(X)`

. Subgroups can have different numbers of
measurements.

**Data Types: **`single`

| `double`

### Name-Value Arguments

Specify optional pairs of arguments as
`Name1=Value1,...,NameN=ValueN`

, where `Name`

is
the argument name and `Value`

is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.

**Example: **`controlchart(X,group,ChartType=["xbar","r"])`

specifies to
create X-bar and R control charts.

*
Before R2021a, use commas to separate each name and value, and enclose*
`Name`

*in quotes.*

`ChartType`

— Chart types

character vector | string scalar | string array | cell array of character vectors

Chart types, specified as a character vector, string scalar, string array, or cell array of character vectors containing one or more of the following values.

Value | Description |
---|---|

`"xbar"` | X-bar or mean |

`"s"` | Standard deviation |

`"r"` | Range |

`"ewma"` | Exponentially weighted moving average |

`"i"` | Individual measurement |

`"mr"` | Moving range of individual measurements |

`"ma"` | Moving average of individual measurements |

`"p"` | Proportion of units that are defective |

`"np"` | Number of defective units |

`"u"` | Defects per unit |

`"c"` | Number of defects |

If you specify `"p"`

or `"np"`

, you
must also specify `Unit`

.

If you specify `"xbar"`

, `"s"`

, or
`"r"`

, then `X`

must be a matrix, or
`X`

must be a vector with subgroups specified in
`group`

.

If you specify more than one element in `ChartType`

, the
elements must be compatible. There are four sets of compatible types.

`"xbar"`

,`"s"`

,`"r"`

, and`"ewma"`

`"i"`

,`"mr"`

, and`"ma"`

`"p"`

and`"np"`

`"u"`

and`"c"`

If you specify `X`

as a vector and do not specify
`group`

, then `"ewma"`

is compatible with
`"i"`

, `"mr"`

, and `"ma"`

.

**Example: **`ChartType={'u','c'}`

**Data Types: **`char`

| `string`

`Display`

— Flag to display control charts

"on" (default) | "off"

Flag to display the control charts, specified as `"on"`

or
`"off"`

.

**Example: **`Display="off"`

**Data Types: **`char`

| `string`

`Label`

— Subgroup labels for plot data tips

string array | cell array of character vectors

Subgroup labels for plot data tips, specified as a string array or a cell array of
character vectors with length equal to `size(X,1)`

(or
`size(X.data,1)`

, if `X`

is a
`timeseries`

object).

**Data Types: **`char`

| `string`

`Lambda`

— Smoothing parameter

`0.4`

(default) | scalar in the range `(0,1)`

Smoothing parameter, specified as a scalar in the range `(0,1)`

.
For the exponentially weighted moving average chart
(`ChartType="ewma"`

), `Lambda`

controls how much
each moving average value is influenced by past measurements. A higher
`Lambda`

value gives more weight to the more recent measurements in
the moving average calculation.

**Example: **`Lambda=0.3`

**Data Types: **`single`

| `double`

`Limits`

— Control limits

3-by-1 numeric vector

Control limits, specified as a 3-by-1 numeric vector. The elements of
`Limits`

contain the lower control limit, the center line, and the
upper control limit values in the control chart, respectively. If a point lies outside
the control limits, the software flags it as out of control (see `plotdata`

).
You cannot specify `Limits`

if you specify more than one chart type
in `ChartType`

.

**Example: **`Limits=[0.5 1 1.5]`

**Data Types: **`single`

| `double`

`Mean`

— mean

`[]`

(default) | numeric scalar

Mean, specified as a numeric scalar or `[]`

. If
`Mean`

is `[]`

(the default), the software
estimates `Mean`

from `X`

. The definition of
`Mean`

depends on the chart type. For the `"p"`

and `"np"`

charts, `Mean`

is the average proportion
of items that are defective. For `"u"`

and `"c"`

charts, `Mean`

is the average number of defects per unit size. For
all other charts, `Mean`

is the process mean.

**Example: **`Mean=0.5`

**Data Types: **`single`

| `double`

`Sigma`

— Standard deviation

`"std"`

(default) | "range" | "variance" | positive scalar

Standard deviation, specified as a positive scalar or a standard deviation estimation method from the following table.

Value | Description |
---|---|

`"std"` (default) | Average within-subgroup standard deviation |

`"range"` | Average subgroup range |

`"variance"` | Square root of pooled variance |

For the `"i"`

, `"mr"`

, and
`"ma"`

chart types, if the data is not in subgroups, the software
computes the estimate using a moving range.

**Example: **`Sigma=0.5`

**Data Types: **`single`

| `double`

| `char`

| `string`

`NSigma`

— Number of standard deviations for control limits

`3`

(default) | positive scalar

Number of standard deviations for control limits, specified as a positive scalar.
The software sets the upper and lower control limits as the center line value plus and
minus `NSigma`

times `Sigma`

, respectively. If a
point lies outside the control limits, the software flags it as out of control (see
`plotdata`

).
If you specify `Limits`

, the software ignores the value of
`NSigma`

.

**Example: **`NSigma=5`

**Data Types: **`single`

| `double`

`Parent`

— Axes handle of control chart plot

[] (default) | handle

Axes handle of the control chart plot, specified as an axes handle. If
`Parent`

is `[]`

(the default), the software
creates the control chart plot in a new figure. You cannot specify
`Parent`

if `ChartType`

contains more than one
chart type.

`Rules`

— Control rules

character vector | string scalar | string array | cell array of character vectors

Control rules, specified as a character vector, string scalar, string array, or
cell array of character vectors containing one or more of the allowed values of
`rules`

in
the `controlrules`

function. The software
uses these rules, together with the control limits, to determine whether a point is
out of control in these chart types: `"xbar"`

,
`"i"`

, `"c"`

, `"u"`

,
`"p"`

and `"np"`

. If you do not specify
`Rules`

, the software uses only the control limits to determine
whether a point is out of control.

**Example: **`Rules=["we1","n1"]`

**Data Types: **`char`

| `string`

`Specs`

— Specification limits

numeric vector

Specification limits, specified as a numeric vector. The software plots a red
horizontal dotted line in the control chart at each value of
`Specs`

. The software does not plot specification lines in these
chart types: `"r"`

, `"s"`

, and
`"mr"`

.

**Example: **`Specs=[0.5 2]`

**Data Types: **`single`

| `double`

`Unit`

— Number of inspected items or size of inspected unit

numeric matrix of positive integers | numeric vector of positive integers | positive integer

Number of inspected items or size of the inspected unit, specified as a numeric
vector or matrix of positive integers that has the same size as
`X`

. You can also specify `Unit`

as a positive
integer. In this case, the software uses the same `Unit`

value for
all the elements of `X`

.

For the chart types

`"p"`

and`"np"`

, you must specify`Unit`

, and its value is the number of inspected items.For the chart types

`"u"`

and`"c"`

,`Unit`

is the size of the inspected unit, and its default value is`1`

.

If you specify `Unit`

for chart type `"p"`

,
`"np"`

, `"u"`

, or `"c"`

, then
`X`

must contain the counts of the number of defects or number of
defective units found. For all other chart types, the software ignores the value of
`Unit`

.

**Example: **`Unit=1`

**Data Types: **`single`

| `double`

`Width`

— Window width

`5`

(default) | positive integer

Window width, specified as a positive integer. The software uses a window of width
`Width`

to compute the moving ranges and averages for the
`"mr"`

and `"ma"`

chart types, and the standard
deviations for the `"i"`

, `"mr"`

, and
`"ma"`

chart types. The value of `Width`

cannot be
larger than `size(X,1)`

(or `size(X.data,1)`

, if
`X`

is a `timeseries`

object).

**Example: **`Width=10`

**Data Types: **`single`

| `double`

## Output Arguments

`stats`

— Subgroup statistics and parameter estimates

1-by-`k`

structure array

Subgroup statistics and parameter estimates, returned as a 1-by-`k`

structure array, where `k`

is the number of plotted charts. The fields
in `stats`

depend on the chart type.

Field | Description |
---|---|

`mean` | Subgroup means |

`std` | Subgroup standard deviations |

`range` | Subgroup ranges |

`n` | Subgroup size, or total inspection size or area |

`i` | Individual data values |

`ma` | Moving averages |

`mr` | Moving ranges |

`count` | Count of defects or defective items |

`mu` | Estimated process mean |

`sigma` | Estimated process standard deviation |

`p` | Estimated proportion of units that are defective |

`m` | Estimated mean defects per unit |

`plotdata`

— plot data

1-by-`k`

structure array

Plot data, returned as a 1-by-`k`

structure array, where
`k`

is the number of plotted charts. The structure array contains the
following fields.

Field | Description |
---|---|

`pts` | Plotted point values |

`cl` | Center line |

`lcl` | Lower control limit |

`ucl` | Upper control limit |

`se` | Standard error of plotted point |

`n` | Subgroup size |

`ooc` | Flags for out of control points, specified as logical
`0` (false) or `1` (true). |

## More About

### Control charts

Shewhart control charts are useful for monitoring the stability of processes over time.
Statistics and Machine Learning Toolbox™ offers several different types of control chart, which you can specify using
the `ChartType`

name-value argument. Each chart is a time series graph of points that represent either the
measured values of a process or a statistic derived from the measurements. The chart
contains a center line that is flanked by two control limit lines that are usually
equidistant from the center line. Any point located outside the region defined by the
control limit lines is flagged as out of control. You can also apply additional statistical
rules to flag out of control points (see `controlrules`

).

The simplest control chart is the `"i"`

(individual) chart, which plots
the measurement values in time order. It is often used alongside an `"mr"`

(moving range) chart, which plots the largest absolute difference between any two of the
last *k* measurements, where *k* is the window width. A
moving average (`"ma"`

) chart plots the moving average over a window of
width *k*. An exponentially weighted moving average
(`"ewma"`

) chart does not use a window, and instead uses a weighted
moving average where newer measurements have higher weights than older measurements.

There is also a set of control charts that are useful for analyzing grouped data. For
example, you might gather *n* measurements of a single unit at regular
intervals over a specific time period. Each set of *n* measurements you
gather at a time *t* is a *subgroup*. An X-bar
(`"xbar"`

) chart plots the mean of each subgroup in time order. This
chart is often used alongside an `"r"`

chart, which plots the largest
absolute difference between any two measurements within each subgroup, or an
`"s"`

chart, which plots the standard deviation of measurements within
each subgroup.

Several charts are specifically designed for logical measurements (for example, whether
a unit is defective or not defective). The `"p"`

and
`"np"`

charts plot the fraction of defective units and the number of
defective units within each subgroup, respectively. For discrete measurements, such as the
number of defects in a unit that has a particular size, you can create a
`"c"`

chart, which plots the total number of defects in each subgroup,
and a `"u"`

chart, which plots the mean number of defects per unit size
within each subgroup.

## References

[1] Montgomery, Douglas C. *Statistical Quality Control*. 7th ed. Nashville, TN: John Wiley & Sons, 2012.

## Version History

**Introduced in R2006b**

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)