What is t-test an why we do it?
A t-test is an analysis of two populations means through the use of statistical examination; a t-test with two samples is commonly used with small sample sizes, testing the difference between the samples when the variances of two normal distributions are not known.
A t-test looks at the t-statistic, the t-distribution and degrees of freedom to determine the probability of difference between populations; the test statistic in the test is known as the t-statistic. To conduct a test with three or more variables, an analysis of variance (ANOVA) must be used. 
Statistical Analysis of the T-Test
The formula used to calculate the test is a ratio: The top portion of the ratio is the easiest portion to calculate and understand, as it is simply the difference between the means or averages of the two samples. The lower half of the ratio is a measurement of the dispersion, or variability, of the scores. The bottom part of this ratio is known as the standard error of the difference. To compute this part of the ratio, the variance for each sample is determined and is then divided by the number of individuals the compose the sample, or group. These two values are then added together, and a square root is taken of the result.
The two-sample t-test is a parametric test that compares the location parameter of two independent data samples.
The test statistic is
where ¯x and ¯y are the sample means, sx and sy are the sample standard deviations, and n and m are the sample sizes.
In the case where it is assumed that the two data samples are from populations with equal variances, the test statistic under the null hypothesis has Student’s t distribution with n + m – 2 degrees of freedom, and the sample standard deviations are replaced by the pooled standard deviation
In the case where it is not assumed that the two data samples are from populations with equal variances, the test statistic under the null hypothesis has an approximate Student’s t distribution with a number of degrees of freedom given by Satterthwaite’s approximation. This test is sometimes called Welch’s t-test.
Test the null hypothesis that the two data vectors are from populations with equal means, without assuming that the populations also have equal variances.
[h,p] = ttest2(x,y,'Vartype','unequal')
h = 0 p = 0.9867
The returned value of
h = 0 indicates that
ttest2 does not reject the null hypothesis at the default 5% significance level even if equal variances are not assumed.