# Creating an epoch from a .mat data file

**URL:** <https://mne.discourse.group/t/creating-an-epoch-from-a-mat-data-file/5308>\
**Category:** Support & Discussions\
**Tags:** data-import\
**Created:** [July 21, 2022, 9:15pm UTC](https://mne.discourse.group/t/creating-an-epoch-from-a-mat-data-file/5308 "2022-07-21T21:15:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsrishyla](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@dsrishyla](https://mne.discourse.group/u/dsrishyla)\
**Post date:** [July 21, 2022, 9:15pm UTC](https://mne.discourse.group/t/creating-an-epoch-from-a-mat-data-file/5308/1 "2022-07-21T21:15:53Z")

</div>

- MNE-Python version: 3.8.8
- operating system: Windows

Hello,

I am trying to convert a .mat data file into an mne epoch object.

I have read the .mat file into mne as follows:

`s_m12_AB_mat_outdata = read_mat('s005_m12_all-epochs_AB_OutData.mat')`

I understand that to convert to an epoch object, I must use mne.EpochsArray, and the first parameter is a three-dimensional data array in the shape of (n\_epochs, n\_channels, times). The data has 111 epochs, 129 channels, each epoch 0-0.998 seconds long, sampling frequency 500 Hz. So the shape must be 111 x 129 x 500. But the .mat file I read in currently has a shape of 129 x 55500. I am trying to convert it to 3 dimensional, so that it can be converted to an epochs object using mne.EpochsArray.

How may I achieve this?

Thank you for your time!

Best,  
Diksha

---

<div class="post-metadata">

**Author:** ![scott-huberty](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/scott-huberty/32/2715_2.png) [@scott-huberty](https://mne.discourse.group/u/scott-huberty)\
**Post date:** [July 22, 2022, 1:57am UTC](https://mne.discourse.group/t/creating-an-epoch-from-a-mat-data-file/5308/2 "2022-07-22T01:57:55Z")

</div>

Hi Diksha,

I’m not very familiar with `pymatreader.read_mat`, but if the data in the `.mat` files were epochs, I’m not exactly sure why your data are shape 129 (channels) x 55500 (samples), although 55500 / 500(Hz) = 111, which is what you suggested the first dimension (n\_epochs) of your data should be.

Since `read_mat` should return a dictionary, what are the dictionary keys you are accessing to get this 129x55500 matrix?

If the data in the .mat files are not epochs but are continuous data, you can follow [this tutorial](https://mne.tools/stable/auto_tutorials/simulation/10_array_objs.html) to first create a `Raw` object, after which epoching the data in mne should be easier.

---

<div class="post-metadata">

**Author:** ![scott-huberty](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/scott-huberty/32/2715_2.png) [@scott-huberty](https://mne.discourse.group/u/scott-huberty)\
**Post date:** [July 22, 2022, 12:17pm UTC](https://mne.discourse.group/t/creating-an-epoch-from-a-mat-data-file/5308/3 "2022-07-22T12:17:03Z")

</div>

It’s a bit hacky, but if you’re sure the data in the .mat files are epoched, and you want to reshape the 2d numpy array to a 3d array of n\_epochs, n\_channels, n\_times, you could do:

`my_3d_arr = np.reshape(my_2d_arr, (129, 111, 500)).swapaxes(0,1) `

You could run a quick test to make sure the reshaped data are what you expect, for example:

`assert my_2d_arr[0,500] == my_3d_arr[1,0,0] # the 501st sample in the 2d_arr should now be the 1st sample of the second epoch in the 3d_arr`

Although I think the better approach is to figure out why the .mat files are being loaded in as 2d arrays 🙂 If the data in the .mat files were epochs, I’d expect them to be a 3d matrix, and I’d expect `read_mat` to convert them to 3d numpy arrays. Although maybe I am working off faulty assumptions.

---

<div class="post-metadata">

**Author:** ![dsrishyla](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@dsrishyla](https://mne.discourse.group/u/dsrishyla)\
**Post date:** [July 22, 2022, 1:02pm UTC](https://mne.discourse.group/t/creating-an-epoch-from-a-mat-data-file/5308/4 "2022-07-22T13:02:56Z")

</div>

Hi Scott - the approach you suggested worked! I tested it using epoch.plot(); I had the original epoched data plotted in MNE. Then I plotted the version from the .mat file converted to epochs using the 2D-3D conversion you suggested, and they both are identical.  
As for why the .mat files are being loaded in as 2d arrays; I am working with an artifact correction algorithm in MATLAB. One of the steps involves converting the format of the input epoched data using the ‘cell2mat’ function. I suspect that might be why.  
Thank you for helping me figure this out 😃
