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:
from pymatreader import read_mat
data = read_mat('newstruct.mat')
I found somewhere this:
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’?
Here is the matlab file I am working on.
An example would help.
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:
- 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['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']['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:
event_labels = np.concatenate(data['F'][0, 0]['events']['label']).tolist()
event_times = data['F'][0, 0]['events']['times'].tolist()
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.
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).
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 that covers steps 3 and 4 above. Step 1 is the most time-consuming part.