Main Content

controlchart

Control charts

Description

controlchart(X) creates an X-bar chart of the measurements in the matrix X. The X-bar chart plots the arithmetic mean of each subgroup, and indicates out-of-control points that are more than three standard deviations above and below the mean of X.

controlchart(X,group) creates an X-bar chart of the measurements in X, with the subgroups specified in group.

controlchart(___,Name=Value) specifies 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, specification limits, and chart type.

example

stats = controlchart(___) returns a structure that contains subgroup statistics and parameter estimates.

example

[stats,plotdata] = controlchart(___) additionally returns a 1-by-k structure array of plotted values, where k is the number of charts specified by ChartType.

example

[stats,plotdata,ax] = controlchart(___) additionally returns graphics handles to the individual plots in ax. (since R2025a)

Examples

collapse all

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 (range) control charts for the data, and return the subgroup statistics.

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

Figure contains 2 axes objects. Axes object 1 with title Control Charts, ylabel X-Bar contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Violation, Center, LCL/UCL. Axes object 2 with ylabel R contains 4 objects of type line. One or more of the lines displays its values using only markers

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 charts, the circled points indicate subgroups that violate the control limits.

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)

Figure contains 2 axes objects. Axes object 1 with title Control Charts, ylabel I contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Violation, Center, LCL/UCL. Axes object 2 with ylabel MR contains 4 objects of type line. One or more of the lines displays its values using only markers

The I chart plots the individual measurement values in sequential order. The MR chart plots the absolute difference between each measurement and the previous measurement. The green center line in each chart represents the mean quantity, and the red lines represent the control limits.

Generate a simulated data set X that contains 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 (proportion of units that are defective) and NP (number of defective units) control charts of the measurements. 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);

Figure contains 2 axes objects. Axes object 1 with title Control Charts, ylabel P contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Violation, Center, LCL/UCL. Axes object 2 with ylabel NP contains 4 objects of type line. One or more of the lines displays its values using only markers

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 because it contains eight failures, which exceeds the upper control limits.

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 (defects per unit) chart of the measurements, and return the subgroup statistics and plot data.

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

Figure contains an axes object. The axes object with title U Control Chart, ylabel U contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Violation, Center, LCL/UCL.

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 the 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±3m/S, where S is sum(unitSize). Display the value of the upper control limit.

Uplot.ucl(1)
ans = 
0.5164

Create a C (number of defects) chart of the measurements, and return the subgroup statistics and plot data.

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

Figure contains an axes object. The axes object with title C Control Chart, ylabel C contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Violation, Center, LCL/UCL.

The C chart plots the number of measured defects on each day. Display the value of the center line μ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 μc+3μc.

Cplot.ucl(1)
ans = 
14.7175

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

Cplot.pts(49)
ans = 
15
Uplot.pts(49)
ans = 
0.5263

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

Input Arguments

collapse all

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.

  • When you specify group, the function 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

Subgroups, 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 function 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 ≤ nlength(X). Subgroups can have different numbers of measurements.

Data Types: single | double

Name-Value Arguments

collapse all

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,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.

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

ValueDescription
"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 a vector with subgroups specified in group.

If you specify more than one element in ChartType, the elements must be compatible according to these 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'}

Example: ChartType=["u","c"]

Data Types: char | string

Flag to display the control charts, specified as "on" or "off". Specify Display="off" and the stats output argument, for example, to return subgroup statistics and not display the control charts.

Example: Display="off"

Data Types: char | string

Subgroup labels for chart 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).

Example: Label=["a","b","c","d","e","f","g"]

Data Types: char | string

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

Control limits, specified as a 3-by-1 numeric vector. The elements of Limits contain the values of the lower control limit, center line, and upper control limit in the control chart, respectively. If a point lies outside the control limits, the function flags it as out of control (see plotdata). You cannot specify Limits if ChartType contains more than one chart type.

Example: Limits=[0.5 1 1.5]

Data Types: single | double

Mean, specified as a numeric scalar or []. If Mean is [] (the default), the function estimates Mean from X. The definition of Mean depends on the chart type. For 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

Standard deviation, specified as a positive scalar or one of the following standard deviation estimation methods:

ValueDescription
"std" (default)Average within-subgroup standard deviation
"range"Average subgroup range
"variance"Square root of pooled variance

For the I, MR, and MA charts, if the data is not in subgroups, the function computes the estimate using a moving range.

Example: Sigma=0.5

Data Types: single | double | char | string

Number of standard deviations for the control limits, specified as a positive scalar. The function sets the upper control limit as the center line plus NSigma times Sigma, and the lower control limit as the center line minus NSigma times Sigma. If a point lies outside the control limits, the function flags it as out of control (see plotdata). If you specify Limits, the function ignores the value of NSigma.

Example: NSigma=5

Data Types: single | double

Axes handle of the control chart, specified as an axes handle. If Parent is [] (the default), the function creates the control chart in a new figure. You cannot specify Parent if ChartType contains more than one chart type.

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 function uses these rules, together with the control limits, to determine whether a point is out of control in these chart types: X-bar, I, C, U, P, and NP. If you do not specify Rules, the function uses only the control limits to determine whether a point is out of control.

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

Data Types: char | string

Specification limits, specified as a numeric vector. The function plots a red horizontal dotted line in the control chart at each value of Specs. The function does not plot specification lines in R, S, and MR charts.

Example: Specs=[0.5 2]

Data Types: single | double

Number of inspected items or size of the inspected unit, specified as a numeric matrix or vector of positive integers that has the same size as X, or as a positive integer. When you specify Unit as a positive integer, the function uses the same Unit value for all the elements of X.

  • For P and NP charts, you must specify Unit, and its value is the number of inspected items.

  • For U and C charts, Unit is the size of the inspected unit, and its default value is 1.

If you specify Unit for P, NP, U, or C charts, then X must contain the counts of the number of defects or number of defective units found. For all other chart types, the function ignores the value of Unit.

Example: Unit=1

Data Types: single | double

Window width, specified as a positive integer. The function uses a window with the specified width to compute the moving ranges and averages for the MR and MA charts, and the standard deviations for the I, MR, and MA charts. 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

collapse all

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.

FieldDescription
meanSubgroup means
stdSubgroup standard deviations
rangeSubgroup ranges
nSubgroup size, or total inspection size or area
iIndividual data values
maMoving averages
mrMoving ranges
countCount of defects or defective items
muEstimated process mean
sigmaEstimated process standard deviation
pEstimated proportion of units that are defective
mEstimated mean defects per unit

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.

FieldDescription
ptsPlotted point values
clCenter line
lclLower control limit
uclUpper control limit
seStandard error of plotted point
nSubgroup size
oocFlags for out-of-control points, specified as logical 0 (false) or 1 (true)

Since R2025a

Axes handles to the individual plots, returned as an array of Axes objects. If you want to return ax, Display must be "on".

More About

collapse all

References

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

Version History

Introduced in R2006b

expand all