File Exchange

image thumbnail


version (39.1 KB) by Giuseppe Cardillo
Calculate the Student t Test for unequal or equal samples size, unpaired or paired samples.


Updated 31 Mar 2018

GitHub view license on GitHub

Student's t test for unpaired or paired samples.
This file is applicable for equal or unequal sample sizes; for paired or
unpaired samples. When the test is unpaired, the Fisher-Snedecor F-test is
performed to assess the equality of variance. If variances are not equal,
Satterthwaite's approximate t test is performed.

X1 and X2 - data vectors (mandatory).
TST - unpaired (0) or paired (1) test (default = 0).
ALPHA - significance level (default = 0.05).
TAIL - 1-tailed test (1) or 2-tailed test (2). (default = 1).
- t value.
- degrees of freedom.
- Confidence interval of means difference (for paired test)
- p-value
- Power
X1=[77 79 79 80 80 81 81 81 81 82 82 82 82 83 83 84 84 84 84 85 ...
85 86 86 87 87];

X2=[82 82 83 84 84 85 85 86 86 86 86 86 86 86 86 86 87 87 87 88 ...
88 88 89 90 90];

Calling on Matlab the function: testt

Answer is:


F DF_numerator DF_denominator p_value
______ ____________ ______________ _______

1.5379 24 24 0.29861

Variances are equal


t DF tail p_value Power
______ __ ____ _________ _______

5.2411 48 1 1.765e-06 0.99958

STATS=TESTT(...) returns a structure with all test(s) statistics

Created by Giuseppe Cardillo

To cite this file, this would be an appropriate format:
Cardillo G. (2006). Student t-Test for unpaired or paired samples.

Cite As

Giuseppe Cardillo (2020). test-t (, GitHub. Retrieved .

Comments and Ratings (11)

How this is different from ttest2 provided by MATLAB?

I do not have the Statistics and Machine Learning Toolbox, so I tried this. Turns out it uses 'fcdf', which requires the same toolbox. Oh well...

Can this work for a one-sample t-test? If so, what do you input for x2?



can the p-value, etc be passed onto variables?

You have partially right. I have fixed the bug and uploaded the new file. Thank you very much!

Rune W Berg

There's a small bug in the procedure that prevents you chose 2-tailed test. Its in line 109.

As far as i can see you need to change it to:

109: if tail ~= 0 && tail ~= 1 && tail ~= 2 %check if tst is 0 or 1 or 2

this m file is good,easy and useful.many thanks Giuseppe

Diego García Bascuñán

If you get the powerStudent, testt works. It's fantastic do it so easily!!!

Giuseppe Cardillo

The file powerStudent is not included because is not a file of mine. As highlighted in the File Details this is a file of Antonio Trujillo Ortiz (file ID: 2907) that you can download.

ifan lin

This m file calls another function: powerStudent, which is not included.



help correction

input parser and table command implementation. Github link

To compute power, Testt requires powerStudent by Trujillo-Ortiz, A. and R. Hernandez-Walls. If this function is not present on the computer, Sandler try to download it from FEX

Changes in description

STATS structure creation

Mistake correction in error handling routines

Fisher-Snedecor F-test check added; Satterthwaite's approximation added; changes in output format

Change in Help section

Improvement in input error handling

Added a TRY...CATCH loop for PowerStudent

minor editing

The file recall the PowerStudent routine (File ID: 2907) by Antonio Trujillo-Ortiz

Error handling improvements

fix bug


fix in errors messages

Improvement of error handling

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux