Main Content

controlchart

Shewhart control charts

Description

controlchart(X) creates an X-bar chart of the measurements in 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(X,group) creates an X-bar chart of the measurements in 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 ≤ nlength(X). Subgroups can have different numbers of measurements.

controlchart(___,Name=Value) 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.

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 in ChartType.

example

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

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);

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 since it contains 8 failures and 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 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 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 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);

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

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.

  • 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

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 ≤ nlength(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.

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.

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

Flag to display the control charts, specified as "on" or "off".

Example: Display="off"

Data Types: char | string

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

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

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

ValueDescription
"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

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

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.

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

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

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

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

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

More About

collapse all

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