function trials = tbs_rseeg_trials(events) % Segments the data into trials %% Create EEG trials EEG = find(strcmp({events.event.type},'RS-EEG')); EEG_LABELS = {'RSpre1','RSpre2','RSpost1','RSpost2','RSpost3'}; chs = 1:length(events.label); for s = 1:length(EEG) tempdata = events; EEG_start = tempdata.event(EEG(s)).sample; EEG_end = tempdata.event(EEG(s)).sample + tempdata.event(EEG(s)).sampdur - 1; index = EEG_start:EEG_end; % Amount of full 1sec segments that can be extracted from trial data total_secs = floor(length(index)/tempdata.fsample); remainder = length(index) - total_secs * tempdata.fsample; % Ignore first <1s remainder of data and keep the rest A = tempdata.trial{1}(chs,index((remainder + 1):end)); B = mat2cell(A,length(chs),repmat(tempdata.fsample,total_secs,1)); tempdata.trial = B; C = tempdata.time{1}(1,index((remainder + 1):end)); D = mat2cell(C,1,repmat(tempdata.fsample,total_secs,1)); tempdata.time = D; for t = 1:length(tempdata.trial) tempevent(t).type = 'eeg'; tempevent(t).value = 254; tempevent(t).sample = index(remainder + 1) + ((t - 1) * tempdata.fsample); tempevent(t).timestamp = tempdata.time{t}(1,1); tempevent(t).sampdur = size(tempdata.trial{t},2); tempevent(t).duration = tempevent(t).sampdur/tempdata.fsample; end tempdata.sampleinfo = []; tempdata.sampleinfo(:,1)= index(remainder + 1):tempdata.fsample:EEG_end; tempdata.sampleinfo(:,2)= [(tempdata.sampleinfo(2:end,1) - 1); EEG_end]; tempdata.event = tempevent; tempdata.block = EEG_LABELS{s}; trials(s) = tempdata; end