[FieldTrip] multiple subject code

Xavier Vrijdag x.vrijdag at auckland.ac.nz
Mon Feb 28 01:31:10 CET 2022

Hi Mubeen,

If your code is working fine for each of the data files individually, you can put it in a for loop with an iterator (k) that has the length of the number of data files. You can automatically construct the list of data files with the dir function. In your code you have to change the line where the cfg.dataset is called to point to the datafile of that iteration. Secondly, you have to add a line at the end of your code (in the loop) to save the results from the calculations into a datastruct. After the loop you can call ft_freqgrandaverage to perform the grand average

It will all look like this:

datapath = 'C:\Users\mubaf\OneDrive\Desktop\MY BRAINSTORM\Cases GGE\’;

datasetlist = dir([datapath '*.edf']);

for k=1:size(datasetlist,1)

        cfg =[];

cfg.dataset    = [datapath datasetlist(k).name];

% your code




cfg.parameter  = ; % enter your parameter

results= ft_freqgrandaverage(cfg,data_combined{:});

It is good habit to call cfg:[]; every time you start configuring the settings for the next step in you analysis as settings of a previous step might influence the next step of your analysis.
To speed up the analysis you might want to consider removing the databrowser step for each iteration to reduce your loop time. Unless you want to see the data in each step.

Happy to help if you have any questions.



From: fieldtrip <fieldtrip-bounces at science.ru.nl> on behalf of mubeen afzal via fieldtrip <fieldtrip at science.ru.nl>
Reply to: FieldTrip discussion list <fieldtrip at science.ru.nl>
Date: Monday, 28 February 2022 at 9:40 AM
To: "fieldtrip at science.ru.nl" <fieldtrip at science.ru.nl>
Cc: mubeen afzal <mubafzal at hotmail.com>
Subject: [FieldTrip] multiple subject code

Hi I have a silly question,

How does one use a folder of edf files of subjects to repeat the code below so that I can perform a grand average in the end? Or does each file have to be run seperately with seperate codes?


cfg            = [];
cfg.dataset    = 'C:\Users\mubaf\OneDrive\Desktop\MY BRAINSTORM\Cases GGE\JJ_05-01-2017.edf';
cfg.channel    = {'EEG Fp1' 'EEG Fp2' 'EEG F7' 'EEG F3' 'EEG Fz' 'EEG F4'...
    'EEG F8' 'EEG T7' 'EEG C3' 'EEG Cz' 'EEG T8' 'EEG P7' 'EEG P3' 'EEG C4'...
    'EEG Pz' 'EEG P4' 'EEG O1' 'EEG O2', 'EEG T3', 'EEG T5', ...
    'EEG T4', 'EEG T6' 'Fp1' 'Fp2' 'F7' 'F3' 'Fz' 'F4' 'F8' 'T7' 'C3'...
    'Cz' 'C4' 'T8' 'P7' 'P3' 'Pz' 'P4' 'O1' 'O2', 'T3', 'T5', 'T4', 'T6'};%'P8'
hdr = ft_read_header(cfg.dataset);
event = ft_read_event(cfg.dataset, 'detectflank', []);

cfg.trialdef.prestim   = 0.5;                   % in seconds
cfg.trialdef.poststim  = 2.5;     % in seconds
cfg.trialdef.eventtype = 'annotation'
cfg.trialdef.eventvalue = 'Ep.sw';
cfg = ft_definetrial(cfg);

datatrials  = ft_preprocessing(cfg);
cfg.continuous              = 'no'
cfg.blocksize  = 10
cfg.channel = 'eeg'
cfg = ft_databrowser(cfg, datatrials)

%power analysis (calls 1 function)
cfg.method = 'mtmfft'
cfg.output ='pow'
cfg.channel  = 'all';
cfg.trials = 'all'
cfg.keeptrials = 'yes' %all trials powerspectrum available if yes in freq.powspctrm
cfg.keeptapers = 'no'
cfg.pad = 'maxperlen'
cfg.polyremoval = 0 %(0 demeans only/1 demeans and detrends/-1 no removal)
cfg.taper = 'hann'
cfg.foilim = [2 50]
freq = ft_freqanalysis(cfg, datatrials);

cfg.variance      = 'yes'
cfg.jackknife     = 'no'
cfg.keeptrials    = 'yes'
cfg.channel       = 'all'
cfg.trials        = 'all'
cfg.frequency     = [2 50]
cfg.latency       = 'all'
[descrip] = ft_freqdescriptives(cfg, freq)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20220228/e36528ba/attachment.htm>

More information about the fieldtrip mailing list