Skip to main content
v2026.11,986 entries · CC-BY 4.0

t Test in MATLAB: ttest and ttest2 Syntax and Output

Run a one-sample, paired or two-sample t-test in MATLAB with ttest and ttest2, and read h, p, ci and stats from the documented output.

Written and maintained by CASRAI Editorial Board

Published

On this page

In MATLAB, run a t-test with ttest for one sample or paired data and ttest2 for two independent samples. Both functions return h (1 if the null hypothesis is rejected at the chosen significance level, 0 if not), a p-value, a confidence interval and a structure with the t statistic, degrees of freedom and standard deviation. Both belong to the Statistics and Machine Learning Toolbox, and both are the functions people mean when they search for a Student’s t-test in MATLAB.

What the t test in MATLAB is used for in research

A t-test compares a mean against a reference value, or compares two means, when the population variance is unknown. In MATLAB the choice of function follows the study design: a single sample against a hypothesised mean, two measurements on the same units (paired), or two separate groups. The MathWorks documentation defines the null hypotheses precisely, and getting the design right matters more than the syntax, because the same two columns of numbers give different answers under ttest (paired) and ttest2 (independent).

The data-table below sets out the five usual cases, the exact call for each, and the null hypothesis the documentation states for it. The null hypothesis for the one-sample test is that the data come from a normal distribution with mean 0 (or with mean m if you supply it) and unknown variance. For the paired test it is that the paired differences x − y have mean zero, which requires x and y to be the same size. For the two-sample test it is that both samples come from normal distributions with equal means.

If you are comparing against how other tools do the same job, the site has parallel walkthroughs for the t-test in R, the t-test in SPSS and the t-test in Excel. For the statistical meaning of the test itself, see Student’s t-test and the p-value.

How to install or access MATLAB’s t-test functions

ttest and ttest2 are documented under Statistics and Machine Learning Toolbox. If the toolbox is not part of your MATLAB installation, add it through the Add-On Explorer, as described in MathWorks’ page on getting add-ons. In releases from R2025a onward, click the Add-Ons icon on the left sidebar (or choose Open more panels, then Add-Ons), search for the product, select it and click Install. In releases before R2025a, go to the Home tab, find the Environment section and click the Add-Ons icon, which opens the Add-On Explorer. Installed add-ons are listed in the Add-Ons panel.

Installing requires a MATLAB licence that covers the toolbox; the access route is the same whether you use MATLAB on a desktop or through your institution’s licence. If you do not have the toolbox, the free alternatives section below lists what GNU Octave provides.

How to run a t test in MATLAB step by step

Step 1: choose the function from the design

  1. One sample against a hypothesised mean, or paired samples (the same units measured twice): use ttest.
  2. Two independent groups: use ttest2.
  3. Put each sample in a numeric vector or matrix of type single or double, the two data types both functions accept.

Step 2: one-sample t-test with ttest

The documentation example loads the stockreturns data set and tests whether the mean of the third column differs from zero:

load stockreturns
x = stocks(:,3);
[h,p,ci,stats] = ttest(x)

To test against a mean other than zero, pass it as the second argument: ttest(x,m). The documented call forms are h = ttest(x), h = ttest(x,y), h = ttest(x,m) and the same forms with Name,Value pairs, with up to four outputs [h,p,ci,stats].

Step 3: paired (Student’s) t-test with ttest(x,y)

Pass both samples to ttest: [h,p,ci,stats] = ttest(x,y). MATLAB tests whether x − y has mean zero, so the two vectors must be the same size and each row must be one matched pair. This is the form people usually mean by “matlab student t test” when they have before-and-after measurements.

Step 4: two-sample t-test with ttest2

The documentation example compares two exam columns:

load examgrades
x = grades(:,1);
y = grades(:,2);
[h,p,ci,stats] = ttest2(x,y)

By default ttest2 assumes the two samples have equal but unknown variances. To drop that assumption, set 'Vartype','unequal':

[h,p,ci,stats] = ttest2(x,y,'Vartype','unequal')

With unequal variances the function uses the Satterthwaite approximation for the degrees of freedom, which is the approach to the Behrens-Fisher problem named in the documentation. Vartype takes one value, even if x is a matrix.

Step 5: set the significance level, direction and dimension

Three Name,Value options apply to both functions. 'Alpha' is the significance level, default 0.05, a scalar between 0 and 1. 'Tail' is 'both' (default, a two-sided test), 'right' or 'left'. For ttest2, 'right' tests whether the mean of x is greater than the mean of y, and 'left' tests whether it is smaller. 'Dim' chooses the dimension to test along; the default is the first non-singleton dimension, so 'Dim',1 tests column means and 'Dim',2 tests row means. For a matrix input, ttest runs a separate t-test along each column and returns a vector of results.

Step 6: write the test in a reproducible script

Keep the call, the options and the data-loading line in one script file so another researcher can rerun it. Record the options you set (Alpha, Tail, Vartype, Dim) alongside the output, because the defaults differ between a pooled and an unpooled analysis and between one- and two-sided tests.

How to read the output of ttest and ttest2

All four outputs have the same meaning in both functions, with one difference in what sd contains.

Output What it contains
h 1 means the null hypothesis is rejected at the Alpha level; 0 means it is not rejected.
p The p-value, a scalar in [0,1]. A small value indicates that the null hypothesis might not be valid.
ci The 100(1 − Alpha)% confidence interval, as a two-element vector of lower and upper bounds. In ttest it is for the population mean (for a paired test, of x − y).
stats.tstat The value of the test statistic.
stats.df The degrees of freedom of the test.
stats.sd The estimated standard deviation. In paired ttest it is the standard deviation of x − y. In ttest2 it is the pooled standard deviation when variances are assumed equal, and a vector of the two unpooled standard deviations when they are not.

Worked example: the one-sample output

The documented ttest(x) result for the stock-returns column is h = 1, p = 0.0106, ci ≈ [−0.7357, −0.0997], tstat ≈ −2.6065, df = 99 and sd ≈ 1.6027. The confidence interval excludes zero and h = 1, so the test rejects a zero mean at the 5% level. The numbers are mutually consistent, which is a useful check on your own output. With df = 99 the sample has 100 observations, so the standard error is 1.6027 / √100 ≈ 0.1603. The midpoint of the interval is about −0.4177, and −0.4177 / 0.1603 ≈ −2.606, which is tstat. Editorial note: this arithmetic is derived from the documented figures to show how the outputs relate; it is not an additional MathWorks result.

Worked example: the two-sample output

The documented ttest2(x,y) result for the two exam-grade columns is h = 0, p = 0.9867, ci ≈ [−1.9438, 1.9771], tstat ≈ 0.0167, df = 238 and sd ≈ 7.7084 (pooled). The interval includes zero and p is far above 0.05, so the test does not reject equal means. Note the degrees of freedom: with equal variances assumed they are a whole number, whereas with 'Vartype','unequal' they come from the Satterthwaite approximation.

How to report it

Report the test statistic, the degrees of freedom and the p-value together, for example t(99) = −2.61, p = 0.011, followed by the confidence interval. Read p as the probability, under the null hypothesis, of a result at least as extreme as the one observed; see p-value and confidence interval for the definitions the site uses. A value of h = 0 means the test did not reject the null hypothesis, which is not the same as showing the means are equal.

Checking the equal-variance choice with vartest2

ttest2 defaults to pooled variances, so many analysts ask whether to check the assumption first. MATLAB’s vartest2 is the two-sample F-test for equal variances (null hypothesis: the two samples have equal variances; data assumed normal) and sits in the same toolbox, so you can run it as h = vartest2(x,y). Treat its result as one input to your decision rather than a gate, since choosing the test after seeing the data affects the error rate of the whole procedure. Using 'Vartype','unequal' from the start avoids having to make that choice. Keep in mind that ttest has no Vartype option: variance handling is a ttest2 decision only.

Troubleshooting t tests in MATLAB

The function is not recognised

ttest and ttest2 are part of Statistics and Machine Learning Toolbox, so an undefined-function error usually means the toolbox is not installed. Use the Add-On Explorer route above.

Paired test fails because x and y differ in size

For ttest(x,y), x and y must be the same size. If your groups have different numbers of observations, they are not paired data and you want ttest2.

The wrong dimension was tested

A matrix is tested column by column by default (the first non-singleton dimension). If your observations run along rows, set 'Dim',2, or transpose the data so each variable is a column.

Missing values

ttest2 treats NaN as missing data and ignores it. For ttest, remove missing values yourself before the call, for example x = x(~isnan(x));, and for a paired test remove the whole pair, not just one member, so that x and y stay the same size.

A one-sided result that looks wrong

If h or p is not what you expected, check Tail. With 'Tail','right' in ttest2, the alternative is that the mean of x is greater than that of y, so swapping the order of the arguments reverses the direction of the test.

Assumptions

The documented null hypotheses assume normally distributed data. With strongly non-normal data or very small samples, consider whether a t-test is the right method before interpreting p.

Free alternatives to MATLAB for a t test

GNU Octave provides functions with the same names through its statistics package, and the documented signatures follow MATLAB’s: [h,pval,ci,stats] = ttest2(x,y,Name,Value), with stats holding the t statistic, degrees of freedom and standard deviation. In Octave, ttest2 ignores NaN values, and the options tail, vartype (equal or unequal), alpha and dim are available. Octave’s older core functions return fewer outputs. The table compares them.

Task MATLAB GNU Octave
One-sample or paired t-test [h,p,ci,stats] = ttest(x) or ttest(x,y) ttest in the statistics package, with the same output list
Two-sample t-test [h,p,ci,stats] = ttest2(x,y) [h,pval,ci,stats] = ttest2(x,y) in the statistics package
Unequal variances 'Vartype','unequal' vartype option set to unequal
Older core function Not applicable t_test and t_test_2, which return [pval,t,df] and assume equal variances

Sources

Data: MATLAB t-test functions by study design

Compares the five usual t-test designs in MATLAB's Statistics and Machine Learning Toolbox: the function and call, the null hypothesis the documentation states, and what the sd output holds. Compiled from the MathWorks ttest, ttest2 and vartest2 reference pages, read 2026-11-17.

Period covered: Documentation read 2026-11-17.

MATLAB t-test functions by study design
DesignMATLAB callNull hypothesis stated in documentationWhat stats.sd holdsSize or variance rule
One samplettest(x) or ttest(x,m)Data are normal with mean 0 (or m) and unknown varianceEstimated standard deviation of xm defaults to 0
Paired samplesttest(x,y)Paired differences x-y have mean zeroStandard deviation of x-yx and y must be the same size
Two samples, equal variancesttest2(x,y)Both samples are normal with equal means; variances equal but unknown (default)Pooled standard deviationVartype is 'equal' by default; NaN ignored as missing
Two samples, unequal variancesttest2(x,y,'Vartype','unequal')Both samples are normal with equal means; variances not assumed equalVector of unpooled standard deviationsSatterthwaite degrees of freedom (Behrens-Fisher problem)
Equal-variance pre-checkvartest2(x,y)Both samples are normal with equal variancesNot applicable (F-test)Separate function in the same toolbox

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about t Test in MATLAB: ttest and ttest2 Syntax and Output

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →