# Parsing EDF dataset to .MAT (matlab file structure)

**URL:** <https://mne.discourse.group/t/parsing-edf-dataset-to-mat-matlab-file-structure/5071>\
**Category:** Support & Discussions\
**Created:** [June 14, 2022, 9:26pm UTC](https://mne.discourse.group/t/parsing-edf-dataset-to-mat-matlab-file-structure/5071 "2022-06-14T21:26:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iuri](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/iuri/32/1509_2.png) [@iuri](https://mne.discourse.group/u/iuri)\
**Post date:** [June 14, 2022, 9:26pm UTC](https://mne.discourse.group/t/parsing-edf-dataset-to-mat-matlab-file-structure/5071/1 "2022-06-14T21:26:25Z")

</div>

I’m trying to parse an EDF dataset to the .MAT file structure described below  
dict\_keys([‘ **header** ’, ‘ **version** ’, ‘ **globals** ’, ‘mrk’, ‘cnt’, ‘nfo’])

The new .MAT file (i.e. **Matlab** format \*.mat) must contain a set variables, and so far, I was able to convert the following:

```python
   sample_rate = raw.info["sfreq"]
   nchannels=len(raw.ch_names)
   nsamples=raw.n_times
   raw.rename_channels({ch: ch.replace('.', '') for ch in raw.ch_names})
   channel_names = raw.ch_names

    labels = np.zeros((1, nsamples), int)
    labels[0, event_onsets] = event_codes

    cl_lab = ['left', 'right']
    nclasses = len(cl_lab)
    nevents = len(event_onsets)

```

now, I must parse the continuous EEG signals, size [time x channels]:

```python
    EEG = m['cnt'].T # Numpy array within the continuous EEG signals, size [time x channels]
    EEG = m['cnt'].T.shape
   (59, 190473)

```

which looks like the array of data below:

```python
array([[-36, -203, -384, ..., -256, -287, -289],
       [-138, -774, -1463, ..., -262, -243, -158],
       [-50, -280, -517, ..., 13, -8, -2],
       ...,
       [-58, -308, -532, ..., -136, -148, -123],
       [-81, -438, -787, ..., -185, -204, -195],
       [-270, -1479, -2709, ..., 106, 193, 322]], dtype=int16)

```

Plus, I need to parse 2 more event related variables: event\_onsets and event\_codes

They are:

```python
   event_onsets = m['mrk'][0][0][0]
   array([[ 2095, 2895, 3695, 4495, 5295, 6095, 6895, 7695,
          8495, 9295, 10095, 10895, 11695, 12495, 13295, 16294,
         17094, 17894, 18694, 19494, 20294, 21094, 21894, 22694,
         23494, 24295, 25095, 25895, 26695, 27495, 30494, 31294,
         32094, 32894, 33694, 34494, 35294, 36094, 36894, 37694,
         38494, 39294, 40094, 40894, 41694, 44693, 45493, 46293,
         47093, 47893, 48693, 49493, 50293, 51093, 51893, 52693,
         53493, 54293, 55093, 55893, 58892, 59692, 60492, 61292,
         62092, 62892, 63692, 64492, 65292, 66093, 66893, 67693,
         68493, 69293, 70093, 73092, 73892, 74692, 75492, 76292,
         77092, 77892, 78692, 79492, 80292, 81092, 81892, 82692,
         83492, 84292, 87291, 88091, 88891, 89691, 90491, 91291,
         92091, 92891, 93691, 94491, 97292, 98092, 98892, 99692,
        100492, 101292, 102092, 102892, 103692, 104492, 105292, 106092,
        106892, 107692, 108492, 111491, 112291, 113091, 113891, 114691,
        115491, 116291, 117091, 117891, 118691, 119492, 120292, 121091,
        121891, 122692, 125691, 126491, 127291, 128091, 128891, 129691,
        130491, 131291, 132091, 132891, 133691, 134491, 135291, 136091,
        136891, 139890, 140690, 141490, 142290, 143090, 143890, 144690,
        145490, 146290, 147090, 147890, 148690, 149490, 150290, 151090,
        154089, 154889, 155689, 156489, 157289, 158090, 158890, 159690,
        160490, 161290, 162090, 162890, 163690, 164490, 165290, 168289,
        169089, 169889, 170689, 171489, 172289, 173089, 173889, 174689,
        175489, 176289, 177089, 177890, 178689, 179489, 182488, 183288,
        184088, 184888, 185688, 186488, 187288, 188088, 188888, 189688]],
      dtype=int32)

    event_onsets.shape
   (1, 200)

```

and:

```python
    event_codes = m['mrk'][0][0][1]
array([[ 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1,
         1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, 1,
         1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, -1,
        -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1,
         1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1,
         1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1,
        -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1,
         1, 1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1,
        -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1,
        -1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, -1,
         1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1,
        -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1,
         1, 1, -1, -1, 1, 1, -1, -1]], dtype=int16)

    event_codes.shape
    (1, 200)

```

Thus, where are the data (in the EDF dataset) corresponding to [‘cnt’] and [‘mkr’] (.MAT) structures?

[‘cnt’].T # Numpy array within the continuous EEG signals, size [time x channels]  
[‘mrk’]: structure of target cue information with fields

Best wishes,

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [June 14, 2022, 10:06pm UTC](https://mne.discourse.group/t/parsing-edf-dataset-to-mat-matlab-file-structure/5071/2 "2022-06-14T22:06:37Z")

</div>

Hello,

> [@iuri](#):
>
> Thus, where are the data (in the EDF dataset) corresponding to [‘cnt’] and [‘mkr’] (.MAT) structures?
> 
> [‘cnt’].T # Numpy array within the continuous EEG signals, size [time x channels]

I suppose this would be `raw.get_data().T`  
As for the mkr thing, no idea.

---

<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:** [June 14, 2022, 11:05pm UTC](https://mne.discourse.group/t/parsing-edf-dataset-to-mat-matlab-file-structure/5071/3 "2022-06-14T23:05:51Z")

</div>

Are you converting to `.mat` format because you want to load it with MATLAB? If so, there are EDF readers for MATLAB already. A quick internet search of `matlab eeg read edf` returned many hits.

---

<div class="post-metadata">

**Author:** ![iuri](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/iuri/32/1509_2.png) [@iuri](https://mne.discourse.group/u/iuri)\
**Post date:** [June 15, 2022, 12:26am UTC](https://mne.discourse.group/t/parsing-edf-dataset-to-mat-matlab-file-structure/5071/4 "2022-06-15T00:26:26Z")

</div>

No.  
In fact, file extension doesn’t even matter at all. The important part is to parse and understand the correct data (from the EDF file) to the correct variables: event\_codes and event\_onsets.

Then, it’d be possible to run a few statistical and CNN algorithms (PSD and LDA).

I tried to use:

```python
events=mne.events_from_annotations(raw)

```

But “events” array, turned to be very different in dimension/shape. So that it confused me.

```python
events[0].shape
(30, 3)

```

while:

```python
event_onsets.shape
    (1, 200)
event_codes.shape
    (1, 200)

```

[event\_codes] is an array of -1 and 1, meaning “left” and “right” to classify the correspondent value located in the same position of array [event\_onsets]. That explains the reason they have the same dimensions:

So, [events] (from mne.events\_from\_annotations) and [event\_codes]/[event\_onsets] have very different dimensions/shapes. However, my intuition tells me that [event\_codes] and [event\_onsets] still can be extracted from [events] (i.e. mne.events\_from\_annotations(raw)):

```python
events=mne.events_from_annotations(raw)
events[0].shape
(30, 2)
array([[656, 0, 2],
       [1968, 0, 3],
       [3280, 0, 3],
       [4592, 0, 2],
       [5904, 0, 2],
       [7216, 0, 3],
       [8528, 0, 3],
       [9840, 0, 2],
       [11152, 0, 3],
       [12464, 0, 2],
       [13776, 0, 2],
       [15088, 0, 3],
       [16400, 0, 2],
       [17712, 0, 3]])

```

So, It’s possible to create [event\_onsets] variable with the 1st column of [events], as well as [event\_codes] with the 3rd column of [events].

How could I generate them, making sure I won’t be missing/losing/mishandling data ?

But that’s just my guess/intuition, and that’s why I posted this thread.

What do you suggest/guess? 😉

Best wishes,  
I

---

<div class="post-metadata">

**Author:** ![iuri](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/iuri/32/1509_2.png) [@iuri](https://mne.discourse.group/u/iuri)\
**Post date:** [June 15, 2022, 12:28am UTC](https://mne.discourse.group/t/parsing-edf-dataset-to-mat-matlab-file-structure/5071/5 "2022-06-15T00:28:57Z")

</div>

Thanks Richard,  
I’ve amended my script and it seems to work.  
I’ve just answered @drammock with enlightening ideas I had, regarding event\_onsets and event\_codes

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [June 15, 2022, 6:44am UTC](https://mne.discourse.group/t/parsing-edf-dataset-to-mat-matlab-file-structure/5071/6 "2022-06-15T06:44:17Z")

</div>

> [@iuri](#):
>
> But “events” array, turned to be very different in dimension/shape

Please see our glossary entry here

[https://mne.tools/stable/glossary.html#term-events](https://mne.tools/stable/glossary.html#term-events)

---

<div class="post-metadata">

**Author:** ![iuri](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/iuri/32/1509_2.png) [@iuri](https://mne.discourse.group/u/iuri)\
**Post date:** [June 15, 2022, 2:58pm UTC](https://mne.discourse.group/t/parsing-edf-dataset-to-mat-matlab-file-structure/5071/7 "2022-06-15T14:58:27Z")

</div>

Right on! Thanks for the documentation  
I was able to convert EDF data and ran CNN and LDA algorithms successfully.  
[EEG\_BCI200\_Motor\_Imagery\_Analysis\_Classification\_Dataset\_1\_EDF.](https://github.com/iuri/bci/blob/main/EEG_BCI200_Motor_Imagery_Analysis_Classification_Dataset_1_EDF.ipynb)  
Best wishes,  
I
