# MATLAB Structure as an MNE RAW object

**URL:** <https://mne.discourse.group/t/matlab-structure-as-an-mne-raw-object/4180>\
**Category:** Support & Discussions\
**Created:** [December 19, 2021, 10:12pm UTC](https://mne.discourse.group/t/matlab-structure-as-an-mne-raw-object/4180 "2021-12-19T22:12:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Subrat\_Bastola](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/subrat_bastola/32/1190_2.png) [@Subrat\_Bastola](https://mne.discourse.group/u/Subrat_Bastola)\
**Post date:** [December 19, 2021, 10:12pm UTC](https://mne.discourse.group/t/matlab-structure-as-an-mne-raw-object/4180/1 "2021-12-19T22:12:34Z")

</div>

Hi, I have a MATLAB structure (.mat) which I wish to save as a mne raw file.I tried loading the structure into python using the pymatreader and want to now view/ save the contents of the structure as an MNE raw file. Is that possible? Could anyone help me with how to convert the desired data as into a `Raw` object using which can later be saved, lets say in fif format? An example would be of great help

This is what I have done so far:

```python
from pymatreader import read_mat

data = read_mat('newstruct.mat')

```

I found somewhere this:  
raw=mne.io.RawArray(dat, info)

How do I exactly find the array that I need to load in ‘dat’ and is there any way I can get all channel information and names and load it into ‘info’?

> **[newstruct.mat](https://drive.google.com/file/d/1CgX50jMn-cDlp6pp1Jlriop-GYYrc14J/view?usp=sharing)**
>
> Google Drive file.

Here is the matlab file I am working on.  
An example would help.

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [December 21, 2021, 3:57pm UTC](https://mne.discourse.group/t/matlab-structure-as-an-mne-raw-object/4180/2 "2021-12-21T15:57:31Z")

</div>

The first step is the hardest, which is figuring out where in the MATLAB struct the various bits of information are stored. Here are some guidelines:

1. After reading your MATLAB struct into Python, figure out which of its entries contain what. I usually start with `sorted(data)` to show the top-level key names, then print the key values one by one (e.g., `data['History']`, `data['ChannelType']`, etc). In your file there is a record array stored in `data['F']` and it will be particularly helpful for you to look at `data['F'][0, 0].dtype` to get its column names, and then go through each entry (e.g., `data['F'][0, 0]['header']`, etc) to continue figuring out what data is stored where. Repeat the process as you find more nested record arrays: `data['F'][0, 0]['events'].dtype`, `data['F'][0, 0]['events']['times']`, etc. For example, `data['F'][0, 0]['events']['times']` has shape (1, 7) where each element is an embedded array; `data['F'][0, 0]['events']['label']` also has shape (1, 7) so presumably the `times` arrays are the times at which each corresponding label occurred. As you figure things out, take “notes” by assigning parts of the struct to descriptive variable names, like:

```python
event_labels = np.concatenate(data['F'][0, 0]['events']['label'][0]).tolist()
event_times = data['F'][0, 0]['events']['times'][0].tolist()

```

1. Once you’ve figured out where in the struct the actual measured signals are, convert it to a NumPy Array with shape (n\_channels, n\_times). Usually it will be clear from the shape of the array (n\_times will usually be much bigger than n\_channels). If the axes are in the wrong order, transpose it.

2. Using the sampling frequency, channel names, and channel types you discovered in your investigation of the struct, create the Info using `mne.create_info()` (see the tutorial linked below).

3. Using the data array and the Info object, use `mne.io.RawArray()` to create the Raw.

There is a tutorial showing how to create our data structures: [Creating MNE-Python data structures from scratch — MNE 0.24.1 documentation](https://mne.tools/stable/auto_tutorials/simulation/10_array_objs.html#sphx-glr-auto-tutorials-simulation-10-array-objs-py) that covers steps 3 and 4 above. Step 1 is the most time-consuming part.
