function trl = mytrialfun(cfg); % this function requires the following fields to be specified % cfg.dataset % cfg.trialdef.eventtype % cfg.trialdef.eventvalue % cfg.trialdef.prestim % cfg.trialdef.poststim hdr = ft_read_header(cfg.dataset); event = ft_read_event(cfg.dataset); markerfile = [cfg.dataset(1:end-3) 'mrk']; trl = []; if exist(markerfile,'file') % read the marker file and make an event for each marker % this depends on the readmarkerfile function that I got from Tom Holroyd % I have not tested this myself extensively, since at the FCDC we % don't use the marker files mrk = readmarkerfileDS(markerfile); for i=1:mrk.number_markers for j=1:mrk.number_samples(i) % determine the location of the marker, expressed in samples trialnum = mrk.trial_times{i}(j,1); synctime = mrk.trial_times{i}(j,2); begsample = (trialnum-1)*hdr.nSamples + 1; % of the trial, relative to the start of the datafile endsample = (trialnum )*hdr.nSamples; % of the trial, relative to the start of the datafile offset = round(synctime*hdr.Fs); % this is the offset (in samples) relative to time t=0 for this trial offset = offset + hdr.nSamplesPre; % and time t=0 corrsponds with the nSamplesPre'th sample % store this marker as an event event(end+1).type = mrk.marker_names{i}; event(end ).value = 0 ; event(end ).sample = begsample + offset; event(end ).duration = 0; event(end ).offset = offset; end end end for i=1:length(event) if strcmp(event(i).type, cfg.trialdef.eventtype) % it is a trigger, see whether it has the right value if ismember(event(i).value, cfg.trialdef.eventvalue) % add this to the trl definition begsample = event(i).sample - cfg.trialdef.prestim*hdr.Fs; endsample = event(i).sample + cfg.trialdef.poststim*hdr.Fs - 1; offset = -cfg.trialdef.prestim*hdr.Fs; trigger = event(i).value; % remember the trigger (=condition) for each trial trl(end+1, :) = [round([begsample endsample offset]) trigger]; end end end