The DEFINETRIAL function allows you to specify your own Matlab function for conditional selection of data segments or trials of interest. That is done using the cfg.trialfun option. Using a trial-function you can use an arbitrary complex conditional sequence of events to select data, e.g. only correct responses, or only responses that happened between 300 and 750 ms after the presentation of the stimulus. You can also use your own reading function to obtain the events, or you can read the data from an EMG channel to detect the onset of muscle activity.
This trial-function should be a Matlab function with the following function definition
function [trl, event] = your_trialfun_name(cfg);
The configuration structure will contain the fields cfg.dataset, cfg.headerfile and cfg.datafile. If you want to pass additional information (e.g. trigger value), then you should do that in the sub-structure cfg.trialdef.xxx. The second output argument of the trialfun is optional, it will be added to the configuration if present (i.e. for later reference).
An example trial-function is below
function [trl, event] = your_trialfun_name(cfg);
% read the header information and the events from the data
hdr = read_header(cfg.dataset);
event = read_event(cfg.dataset);
% search for "trigger" events
value = [event(find(strcmp('trigger', {event.type}))).value]';
sample = [event(find(strcmp('trigger', {event.type}))).sample]';
% determine the number of samples before and after the trigger
pretrig = -cfg.trialdef.pre * hdr.Fs;
posttrig = cfg.trialdef.post * hdr.Fs;
% look for the combination of a trigger "7" followed by a trigger "64"
% for each trigger except the last one
trl = [];
for j = 1:(length(trigger)-1)
trg1 = trigger(j);
trg2 = trigger(j+1);
if trg1==7 && trg2==64
trlbegin = sample(j) + pretrig;
trlend = sample(j) + posttrig;
offset = pretrig;
newtrl = [trlbegin trlend offset];
trl = [trl; newtrl];
end
end