clear; clc; load('C:\Users\IONSLAB\Downloads\eeg.event_examplemat.mat'); EEG = EEG_example; stim_codes = [11:16 21:26 31:36 41:46 51:56 61:66]; % Stim code shown at time zero % resp_codes = 71:74; % accuracy response given somewhere between stimulus offset and time +1500ms awareness_codes = 81:84; % awareness report given somwhere between accuracy response+300ms and response+2000ms %Create index of stimulus events indx_stim = ismember([EEG.event.type], stim_codes) > 0; %Create index of awareness events indx_awareness = ismember([EEG.event.type], awareness_codes) > 0; counter = 1; disp('Processing events...'); for iEvent = 1:length(indx_stim) %Find which event is 1st stimulus of current epoch epoch = EEG.event(iEvent).epoch; indx_epoch = find([EEG.event.epoch] == epoch); stim = min(indx_epoch); %If current event is the 1st stimulus, export in new_events if indx_stim(iEvent) == true && iEvent == stim new_events(counter).stim = EEG.event(stim).type; new_events(counter).lat_stim = 0; %Then, get the time distance between each awareness response and this stimulus for iAware = 1:length(indx_awareness) if indx_awareness(iAware) == true distance(iAware) = EEG.event(iAware).latency - EEG.event(iEvent).latency; if distance(iAware) < 0 distance(iAware) = NaN; %dont include event with negative distance as they are from previous trial end else distance(iAware) = NaN; end end %Get closest awareness response (i.e. match) match = find(distance == min(distance)); if EEG.event(stim).epoch == EEG.event(match).epoch %Make sure it is from same epoch %get wareness event info in same epoch as corresponding stimulus: new_events(counter).awareness = EEG.event(match).type; new_events(counter).lat_awareness = EEG.event(match).latency - EEG.event(iEvent).latency; new_events(counter).epoch = counter; %Combination of both stim and awareness response new_events(counter).comb = [num2str(EEG.event(iEvent).type) num2str(EEG.event(match).type)]; counter = counter + 1; end end end disp('Done.');