[FieldTrip] Grand Average on time lock data

Roey Schurr roeysc at gmail.com
Wed Sep 3 08:59:06 CEST 2014


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>
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> 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
> > http://mailman.science.ru.nl/mailman/listinfo/fieldtrip
> _______________________________________________
> fieldtrip mailing list
> fieldtrip at donders.ru.nl
> http://mailman.science.ru.nl/mailman/listinfo/fieldtrip
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20140903/279e63a8/attachment.html>


More information about the fieldtrip mailing list