[FieldTrip] Grand Average on time lock data

"Jörn M. Horschig" jm.horschig at donders.ru.nl
Wed Sep 3 10:29:43 CEST 2014


Hi all,

there is also a feature in FieldTrip to load or save data, which is 
supported by nearly all functions (if you find one which does not 
support it and it should, let us know). You can set cfg.inputfile = 
'filename' for loading data or cfg.outputfile = 'filename' for saving 
data. Note, however, that cfg.inputfile is to my knowldge exclusively 
compatible to having used cfg.outputfile, i.e. if the variable that is 
saved/loaded has a different name than what FieldTrip expects, this 
won't work. It might be worth a try for you guys, because then 
everything is 'in Fieldtrip style' (and more importantly, history is 
kept on what data was loaded in data.cfg.previous).

Apart from that, thanks Roey for sharing your code! I am sure people 
will use your function ;)

Best,
Jörn

On 9/3/2014 8:59 AM, Roey Schurr wrote:
> Dear fieldtrippers,
>
> I found it uncomfortable having to know the names of different 
> variables as I load them, so I have written a function called 
> "load2struct" (see below).
> It should be used in cases in which the mat file holds only one 
> variable (a volume, a grid, etc), otherwise only the first variable is 
> loaded. In your example, Emilie, you could use:
>
> avgs{iFile} = load2struct(datafiles{iFile});
>
> For me it helped increase the code readability, and allowed my codes 
> to be more compatible with changes I did along the way, like changing 
> the naming format for variables used for specific subjects.
> I hope you find it useful (and also that it's not silly (maybe there's 
> a much simpler solution), or just wrong).
>
> Best,
> Roey
>
>
> ===========================================================
> function struct = load2struct(filePath)
>     % LOAD2STRUCT loads the struct in the filePath mat file to a variable,
>     % without needing to know the original field name of this struct.
>     struct = load(filePath);
>     structFieldName = fieldnames(struct);
>     if (length(structFieldName) > 1)
>         warning('Note: load2struct only loads the first variable in 
> the mat file')
>     end
>     eval(['struct = struct.' structFieldName{1} ';']);
> end
>
>
> On Mon, Sep 1, 2014 at 10:28 AM, Eelke Spaak 
> <eelke.spaak at donders.ru.nl <mailto:eelke.spaak at donders.ru.nl>> wrote:
>
>     Dear Emilie,
>
>     If you assign the return value of MATLAB's load() to a variable, that
>     variable will become a struct with the variables in the specified
>     mat-file as its fields. In your case avgs{k} will probably (have not
>     read your script very thoroughly) be a struct, so avgs{k}.avgFC is the
>     actual timelock structure. You should replace
>
>     avgs{iFile} = load(datafiles{iFile});
>
>     with
>
>     tmp = load(datafiles{iFile});
>     avgs{iFile} = tmp.avgFC;
>
>     and then I think it should work.
>
>     Best,
>     Eelke
>
>     On 1 September 2014 09:02, Caspar, Emilie <e.caspar at ucl.ac.uk
>     <mailto:e.caspar at ucl.ac.uk>> wrote:
>     > Dear all,
>     >
>     >
>     > I would like to perform the grand average in a separate script
>     than the
>     > preprocessing one, to avoid a crash, because of the number of
>     participants.
>     > In the preprocessing script, I performed a timelockanalysis for each
>     > participants : avgFC = ft_timelockanalysis(cfg, cleandata);
>     > and then save this file :
>     > finfname  = [subjID{s} '_RP'];
>     > mkdir(subjID{s})
>     > save([subjPath filesep finfname '.mat'], 'avgFC');
>     >
>     >
>     > So, in my grand average script, I wrote the following :
>     >
>     > cfg = [];
>     > homePath    = '/Users/emilie/Desktop/EEG_RP/Grandaverage/';
>     > eegPath = homePath;
>     >
>     >
>     >
>     > %% Subjects
>     > datafiles      =
>     >
>     {'S83_RP','S84_RP','S85_RP','S87_RP','S88_RP','S89_RP','S90_RP','S91_RP','S92_RP','S93_RP','S94_RP','S97_RP','S98_RP','S99_RP','S100_RP',
>     > 'S101_RP'};
>     > nFiles      = length(datafiles);
>     > avgs = cell(1,nFiles);
>     >
>     >
>     >
>     > cfg.channel   = 'all';
>     > cfg.latency   = 'all';
>     > %cfg.parameter = 'avg';
>     >
>     >
>     >
>     > for iFile = 1:nFiles
>     >     thisfile = datafiles{iFile};
>     >     subjPath = [eegPath  thisfile];
>     >     file = dir([subjPath  '*.mat']);
>     >     avgs{iFile} = ft_checkdata(avgs{iFile}, 'datatype', 'timelock',
>     > 'feedback', 'no');
>     >     avgs{iFile} = load(datafiles{iFile});
>     > end
>     >
>     >
>     >
>     > GA = ft_timelockgrandaverage(cfg, avgs{:});
>     >
>     > But I'm not pretty sure to understand the error message :
>     > Error using ft_checkdata (line 366)
>     > This function requires timelock data as input.
>     >
>     > Error in Averaging (line 33)
>     >     avgs{iFile} = ft_checkdata(avgs{iFile}, 'datatype', 'timelock',
>     > 'feedback', 'no');
>     >
>     > Because If I'm right, the files in 'datafiles' are the time
>     locked data from
>     > the preprocessing script.
>     >
>     > Thank you,
>     >
>     > Emilie
>     >
>     >
>     > _______________________________________________
>     > fieldtrip mailing list
>     > fieldtrip at donders.ru.nl <mailto:fieldtrip at donders.ru.nl>
>     > http://mailman.science.ru.nl/mailman/listinfo/fieldtrip
>     _______________________________________________
>     fieldtrip mailing list
>     fieldtrip at donders.ru.nl <mailto:fieldtrip at donders.ru.nl>
>     http://mailman.science.ru.nl/mailman/listinfo/fieldtrip
>
>
>
>
> _______________________________________________
> fieldtrip mailing list
> fieldtrip at donders.ru.nl
> http://mailman.science.ru.nl/mailman/listinfo/fieldtrip


-- 
Jörn M. Horschig
PhD Student
Donders Institute for Brain, Cognition and Behaviour
Centre for Cognitive Neuroimaging
Radboud University Nijmegen
Neuronal Oscillations Group
FieldTrip Development Team

P.O. Box 9101
NL-6500 HB Nijmegen
The Netherlands

Contact:
E-Mail: jm.horschig at donders.ru.nl
Tel:    +31-(0)24-36-68493
Web: http://www.ru.nl/donders

Visiting address:
Trigon, room 2.30
Kapittelweg 29
NL-6525 EN Nijmegen
The Netherlands




More information about the fieldtrip mailing list