Change these to appropriate paths for your operating system and setup

```
addpath /home/lau/matlab/fieldtrip-20170613_workshop_natmeg/
ft_defaults
raw_meg_path = '/archive/20067_workshop_source_reconstruction/MEG/';
meg_path = '/home/share/workshop_source_reconstruction/data/MEG/';
mri_path = '/home/share/workshop_source_reconstruction/data/MRI/';
```

Make subject and recording specific paths (the cell array “subjects_and_dates” can be expanded)

```
%% subjects and dates
subjects_and_dates = ...
{
'NatMEG_0177/170424/'
};
output_path = fullfile(meg_path, subjects_and_dates{1});
cd(output_path)
events = [1 2 4 8 16]; %% right little, right ring, right middle, right index, right thum
```

Minimum-norm estimate (MNE) use a cortical surface as source model. We will not run the preparation of the cortical surface as it takes about 10 hours. You can find tutorial documentation on how the cortical surface source model was made here: http://natmeg.se/onewebmedia/prepare_MNE_sourcespace.html. In the tutorial, you can read how to export MRI to Freesurfer to extract the cortical surface, and set up a source model with MNE-C. This creates a set of point equally sampled across the cortical surface. These points are our source model for MNE.

You will find the file “Sub02-oct-6-src.fif” in the tutorial data files.

We read the MNE-C source space into MATLAB by using *ft_read_headshape* from the MRI folder:

```
cd(fullfile(mri_path,'freesurfer/Sub02/bem')) % This should change if looping over subjects
sourcemodel = ft_read_headshape('Sub02-oct-6-src.fif', 'format', 'mne_source');
cd(fullfile(meg_path,subjects_and_dates{1}))
```

- Q: What does the “sourcemodel” structure contain?

Note that the units are in meters. You can use ft_convert_units to make the units milimeters instead:

```
cd(fullfile(/home/share/workshop_source_reconstruction/data/MRI/))
sourcemodel = ft_convert_units(sourcemodel, 'mm');
```

You can plot the source model with the function ft_plot_mesh:

`figure; ft_plot_mesh(sourcemodel, 'edgecolor', 'k'); camlight `

Looks nice!

Each point on the grid will represent a dipole in the MNE source reconstruction.

- Q: How many dipoles will the MNE source reconstruction contain?

```
load headmodel_mne_meg.mat
load timelockeds.mat
```

Now you should have the three main ingredients for doing MNE source reconstruction with MEG:

- Evoked MEG data.
- A volume model of the brain.

- A source space model of the cortical surface.

Take a look at the headmodel and sourcemodel toghether:

```
figure; hold on
ft_plot_vol(headmodel_mne_meg, 'facealpha', 0.5, 'edgecolor', 'none');
ft_plot_mesh(sourcemodel, 'edgecolor', 'k'); camlight
```

- Q: What is going on – explain the picture?

We have aligned the MRI that we used to create the volume model to the Neuromag coordinate system used by the MEG scanner. However, to make the cortical surface in Freesurfer, we had to convert the MRI to the spm coordinate system and exported it to Freesurfer (see http://natmeg.se/onewebmedia/prepare_MNE_sourcespace.html). When we imported the source space back into MATLAB, it kept this coordinate system. Our head model and source space are now in two different coordinate systems.

To get the cortical surface back into the head, you need to transform the points in source space to the coordinate system of the head model. Load the transformation matrix of the resliced MRI in the spm coordinate system (*transform_vox2spm_rs*). We will use this to create a transformation from the spm coordinate system to the neuromag coordinate system. If you have run the prepare MNE source space tutorial, you must read the transformation files you created yourself.

```
% from voxel to spm-coordsys.
load transform_vox2spm.mat
% Load segmented mri and get transform
load mri_segmented_mne.mat
% Get transformation matrix from voxel to neuromag-coordsys
transform_vox2neuromag = mri_segmented_mne.transform;
% Get transformation matrix from spm to neuromag
T = transform_vox2neuromag/transform_vox2spm_rs;
```

Now transform the source space using the transformation matrix **T**. Note, that if you run the following line several times, it will apply the transformation each time, transforming from the current position to a new location, which will be wrong. If in doubt, save the source model before proceeding.

`sourcemodelT = ft_transform_geometry(T, sourcemodel);`

Be aware that each time you run *ft_transform_geometry* it will apply the transform **T** to the coordinated of the points in the source model.

Take a look at the headmodel and sourcespace again:

```
figure; hold on
ft_plot_vol(headmodel_mne_meg, 'facealpha', 0.5, 'edgecolor', 'none');
ft_plot_mesh(sourcemodelT, 'edgecolor', 'k'); camlight
```