function [trl, event] = ft_trialfun_example1(cfg)

% FT_TRIALFUN_EXAMPLE1 is an example trial function. It searches for events
% of type "trigger" and specifically for a trigger with value 7, followed
% by a trigger with value 64.
% 
% You can use this example trial function as template for your own
% conditial trial definitions.
%
% See also FT_DEFINETRIAL, FT_PREPROCESSING

% read the header information and the events from the data
% this should always be done using the generic read_header
% and read_event functions
hdr   = ft_read_header(cfg.dataset);
event = ft_read_event(cfg.dataset);

% search for "trigger" events
trigger = [event(strcmp('TRIGGER', {event.type})).value]';
sample  = [event(strcmp('TRIGGER', {event.type})).sample]';

% determine the number of samples before and after the trigger
pretrig  = -round(cfg.trialdef.prestim  * hdr.Fs);
posttrig =  round(cfg.trialdef.poststim * hdr.Fs);

% look for the combination of a trigger "7" followed by a trigger "64" 
% for each trigger except the last one
trl = [];
delay    = hdr.Fs/1000*70;
delay = round(delay);
trigger=trigger(1:2:end);
sample=sample(1:2:end);
for j = 1:length(trigger);
  trg1 = trigger(j);
  if trg1==4098 
    trlbegin = sample(j) + pretrig;       
    trlend   = sample(j) + posttrig;       
    offset   = pretrig;
    newtrl   = [trlbegin trlend offset];
    newtrl   = newtrl+delay;
    trl      = [trl; newtrl];
  end
end

