function [data_epoch] = maketrials_eyetracking_V1(cfg) %First input = data after importeyes; second input = trigger mdat=cfg.data; alltriggers= cfg.triggers; pretim = cfg.prestim; posttim = cfg.poststim; tmp=mdat.evt; evt=zeros(length(tmp),1); for i=1:length(tmp) if isempty(str2num(tmp{i})) evt(i)=NaN; else evt(i)=str2num(tmp{i}); end end tim=mdat.evT; tcounter=1; % Make epochs for tri=1:length(alltriggers) trigger=alltriggers(tri); found=find(evt==trigger); % tim(found(ttt)) for ttt = 1:length(found) onset= tim(found(ttt)); [ d1, ix1 ] = min( abs( mdat.time-(onset-pretim) ) ); [ d1, ix2 ] = min( abs( mdat.time-(onset+posttim) ) ); oritime_nosort(tcounter,:)= mdat.time(ix1:ix2) ;%original time eyedat_nosort(tcounter,1,:)= mdat.x(ix1:ix2); %x eyedat_nosort(tcounter,2,:)= mdat.y(ix1:ix2); %y eyedat_nosort(tcounter,3,:)= mdat.p(ix1:ix2); %p trigger_onset_nosort(tcounter)=(trigger); tcounter=tcounter+1; end end %sorting everything by time [A I]= sort(oritime_nosort(:,1)) data_epoch.originaltime=[] data_epoch.eyedat=[] data_epoch.trigger_onset=[] data_epoch.eyedat= eyedat_nosort(I,:,:); data_epoch.originaltime= oritime_nosort(I,:); data_epoch.trigger_onset=trigger_onset_nosort(I); data_epoch.channel=['x';'y';'p';]; %make the time for the epoch t1=length(-pretim:posttim)-1 t2=size(data_epoch.eyedat,3)-1 spac=t1/t2 data_epoch.time=-pretim:spac:posttim; end