<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>FieldtripAnalysis</title><revhistory><revision><revnumber>4</revnumber><date>2013-03-07 21:23:04</date><authorinitials>localhost</authorinitials><revremark>converted to 1.6 markup</revremark></revision><revision><revnumber>3</revnumber><date>2011-01-27 17:51:36</date><authorinitials>JonathanPeelle</authorinitials></revision><revision><revnumber>2</revnumber><date>2011-01-26 17:57:52</date><authorinitials>JonathanPeelle</authorinitials></revision><revision><revnumber>1</revnumber><date>2011-01-26 17:37:24</date><authorinitials>JonathanPeelle</authorinitials></revision></revhistory></articleinfo><section><title>Analyzing MEG data in FieldTrip</title><para>A much more thorough and complete guide can be found at <ulink url="http://fieldtrip.fcdonders.nl/tutorial"/>.  This is intended to give people who haven't used <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink> at all an overall feel for how data is structured and analyses are conducted. </para><section><title>How FieldTrip compares to SPM</title><para>Like SPM, <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink> is written in Matlab.  Unlike SPM, there is no graphical interface for conducting analyses; everything is done through scripts.  This provides a great deal of flexibility, and the ability to always be able to see exactly what analyses were run.  However, it does take a bit of knowledge of Matlab (but not too much). </para><para>(SPM now includes a distribution of <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink>, so many of the &quot;behind the scenes&quot; analyses are identical.) </para><para>Because <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink> functions are written in Matlab, to get help on any function you can type &quot;help &lt;function&gt;&quot; in Matlab, or &quot;edit &lt;function&gt;&quot; to view the source (and help).  It's a good habit to get into to look at the help for every function you use. </para></section><section><title>Matlab structures</title><para>Most <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink> functions take as one of their arguments a configuration (cfg) structure.  A structure is a Matlab variable that contains one or more fields, and each field can then in turn contain a type of information.  An empty structure can be created using struct([]): </para><screen><![CDATA[>> c = struct([])
]]><![CDATA[
c = 
]]><![CDATA[
0x0 struct array with no fields.
]]><![CDATA[
>> isstruct(c)
]]><![CDATA[
ans =
]]><![CDATA[
     1
]]><![CDATA[
>> ]]></screen><para>By convention, <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink> example scripts usually start by creating an empty matrix using [], and then assign some values to turn it into a structure: </para><screen><![CDATA[>> c = []
]]><![CDATA[
c =
]]><![CDATA[
     []
]]><![CDATA[
>> isstruct(c)
]]><![CDATA[
ans =
]]><![CDATA[
     0
]]><![CDATA[
>> c.myfield = [1 2 3]
]]><![CDATA[
c = 
]]><![CDATA[
    myfield: [1 2 3]
]]><![CDATA[
>> isstruct(c)
]]><![CDATA[
ans =
]]><![CDATA[
     1
]]><![CDATA[
>> ]]></screen><para>Now that we have a structure called c, we can add some fields to it using a dot notation (i.e., &lt;structure&gt;.&lt;field&gt;): </para><screen><![CDATA[>> c.field1 = 1;
>> c.field2 = 3.4;
>> c.you_can_name_them_whatever_you_want = 'structures are fun';
>> c
]]><![CDATA[
c = 
]]><![CDATA[
                                myfield: [1 2 3]
                                 field1: 1
                                 field2: 3.4000
    you_can_name_them_whatever_you_want: 'structures are fun'
]]><![CDATA[
>> ]]></screen><para>Note that fields can take any kind of information: vectors, matrices, integers, strings...even other structures.  From the perspective of <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink>, this makes it easy to pass a lot of information to a function using only a single variable. </para></section><section><title>Starting FieldTrip</title><para>To get started in <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink>, you just need to make sure that all of <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink>'s functions are in your Matlab path.  To do so, make sure the main <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink> distribution is in your path: </para><screen><![CDATA[>> addpath /imaging/local/software/fieldtrip/fieldtrip-current]]></screen><para>and then run the defaults function: </para><screen><![CDATA[>> ft_defaults]]></screen><para>Now you're ready to go! </para></section><section><title>Reading in data</title><para>For most things you do in <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink>, it's probably worth saving the scripts so that you can easily change options and see what you've done. </para><screen><![CDATA[cfg = [];                          % initialize a cfg variable
cfg.dataset = 'td4sblock1.fif';    % specify the (post-maxfilter, probably) datafile
cfg.trialdef.eventtype = 'trial';  % tell FT what kind of events to get
cfg = ft_definetrial(cfg);         % find the events
data = ft_preprocessing(cfg);      % get the data with default options]]></screen><para>In this case, note that the cfg structure is updated by ft_definetrial, and then for ft_preprocessing we are using this new cfg structure. </para><para>You may end up with multiple data files (e.g., one per condition), which you can then go on to compare. </para><para>== An example script ==  </para><para>Here is a slightly longer example toy script that includes basic artifact rejection, time-locked averaging, and topographic plotting.  <ulink url="https://imaging.mrc-cbu.cam.ac.uk/imaging/FieldtripAnalysis/imaging/FieldTrip#">FieldTrip</ulink> also includes extensive options for doing statistics using permutation testing, which are worth looking into. </para><screen><![CDATA[% (FieldTrip uses the MNE matlab toolbox for reading in data files; it
% should be on your path by default, but when I ran this it wasn't...)
% addpath /opt/mne/matlab/toolbox
]]><![CDATA[
clear all
]]><![CDATA[
%% define the dataset
cfg = [];
cfg.continuous = 'yes';
cfg.dataset = '08_0424_td4sblock1.fif';
]]><![CDATA[
]]><![CDATA[
%% set up the trial definition, and define trials
cfg.trialdef.eventtype = 'STI101';
cfg.trialdef.eventvalue = 9;
cfg.trialdef.prestim = .1; % 100 ms pre-stimulus included as baseline
cfg.trialdef.poststim = 1; % keep 1000 ms of data
cfg = ft_definetrial(cfg);
]]><![CDATA[
%% reject artifacts based on EOG and sensor jumps using defaults
cfg.artfctdef.eog.channel = {'EEG061';'EEG062'};
cfg = ft_artifact_eog(cfg);
]]><![CDATA[
cfg.artfctdef.jump.channel = {'MEG'};
cfg = ft_artifact_jump(cfg);
]]><![CDATA[
% now remove any bad sections from definition
cfg = ft_rejectartifact(cfg)
]]><![CDATA[
]]><![CDATA[
%% read in the data (not including rejected trials)
cfg.channel = {'MEG'};
cfg.lpfilter = 'yes';
cfg.lpfreq = 50;
cfg.hpfilter = 'yes';
cfg.hpfreq = 0.2;
data = ft_preprocessing(cfg);
]]><![CDATA[
]]><![CDATA[
%% compute a grand average
cfg = []; % we can use a new cfg
timelock = ft_timelockanalysis(cfg, data);
]]><![CDATA[
]]><![CDATA[
]]><![CDATA[
%% plot the grand average for one channel
cfg.channel = {'MEG0222'};
ft_singleplotER(cfg, timelock)
]]><![CDATA[
]]><![CDATA[
%% topographic plot at 100 ms
cfg = [];
cfg.xlim = [.1 .1]; % around 100 ms
ft_topoplotER(cfg, timelock)]]></screen></section></section></article>