In order to calculate the T2 relaxation time you need to acquire multiple T2 weighted images (Spin Echo sequence) in one of the following two ways:
1- Run the same spin echo sequence multiple times, in each repeat you only need to change the echo time
2- Run one multi-echo sequence one with multiple TEs
In both ways, you need to do a pixel-by-pixel fitting for your images to get the T2 relaxation time. This can be done in Matlab or in any other image analysis softwares
Firstly, you need to have multiple T2 weighted images as Ali said.
After that you will need to reconstruct T2 mapping images by using the pixel-by-pixel fitting to calculate the T2 value. It certainly can be done by MatLab. If you are using Siemens MRI scanner, the software 'syngo MapIt' can do that as well.
A simple approach called T2 relaxation rate is used in this paper: doi:10.1111/j.1528-1167.2006.00916.x. A TE=20 (PD) image and a TE=80 (T2) image only is used for calculation.
An approach called voxel based relaxometry (VBR) is suggested here: doi:10.1016/j.neuroimage.2003.09.059
Please select the TE values in such a way that the T1 influence on the data is eliminated. Keep TEs on the lower side. For approx. estimation simple monoexponential plot of TE & SI will give you T2 value.
To add to Mustapha's response above, there is extensive research done with the CPMG sequence acquiring up to 32 subsequent echoes to characterize the decay curve accurately (Whittle et.al DOI: 10.1002/mrm.1910370107).
In recent years there have also been development using a GRASE sequence (Gradient Spin Echo) where a quick EPI read out during each spin echo can accelerate data acquisition by a factor of 3. See DOI: 10.1016/j.neuroimage.2012.06.064
A pointer for the analysis as mentioned above. If you are trying to characterize the T2 distribution in a voxel with multiple T2 values (which in tissue is the case) there are some more sophisticated methods to do this. There are also confounding factors such as inaccurate refocusing pulses that will influence your measured T2 values, called stimulated echoes. See DOI: 10.1002/mrm.23157 for an example of such analysis.