Stimulation delays

Hi

I have to adjust my triggers for stimulation delays. I saw the option for doing this through Brainstorm (Events>time offset) but this seems to require the same offset correction across trials. I have data from a cognitive task which has 4 categories (original_11, original_12, original_21 & original_22), each with a 100 trials so a total of 400 trials per subject. The offset delay varies from trial to trial. Could you advise how I can amend individual trials?

I have the delay for each trial for each subject stored in a table 'T'. T.Subject has all subject IDs (400 rows for each subject), T.Conditions has the categories (11, 12, 21 & 22 (100 rows per category)), T.Trial lists the number of the trial in each category (1-100) and T.Delay has all the delays that the 'original' time stamp has to be corrected for. I looked at doing this through MatLab but I'm not sure how to match between a table and a struct so trials are altered appropriately?

sFiles = bst_process('CallProcess', 'process_select_search', sFiles, [], ...
'search', '(([name CONTAINS "resample"]))');
for iFile = 1:length(sFiles)
DataMat = in_bst_data(sFiles(iFile).FileName);

original_11 = DataMat.F.events(1).times
original_12 = DataMat.F.events(2).times
original_21 = DataMat.F.events(5).times
original_22 = DataMat.F.events(6).times

end

sample rows form T
T =
Subject Conditions Trial Delay
"Subject008" 11 95 83
"Subject121" 12 96 84
"Subject156" 21 97 67
"Subject356" 22 98 67

This is too specific to handle it from the interface.
The easiest and most efficient solution is probably write a script that edits directly the events structure from the original continuous file:
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Example:_Editing_events

Events structures are documented here:
https://neuroimage.usc.edu/brainstorm/Tutorials/EventMarkers#On_the_hard_drive

Hi Francois,

Thank you. I will try developing a script using these sources. Reading through that material and looking at other sources/forums for MatLab I'm not sure how to 'match' variables between the table and struct e.g. match subject, event category and trial. Could you advise what function I should look at for this?

If you are comfortable with Matlab programming, you should start by looking for Matlab tutorials teaching you how to write for loops, if/else tests and strcmp/strcmpi for testing the equality between two strings.

Hi

This script allowed me to add delays to individual trials. I ran this after running a script to create a new event with offset as in the tutorial to edit events (link below). I had an Excel file (delay_triggers), with four columns for subject ID, event categories (I had four categories, below is for one), trial number within event category and delays (in ms).

https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Example:_Editing_events

t = readtable('delay_triggers')

[testID, SubjectNames] = findgroups(t.Subject);
[testID, Events] = findgroups(t.Event);
Events = num2cell(Events)

% create struct for delays

for i = 1:length(SubjectNames)
for i2 = 1:length(Events)

B(i).Subject = string(SubjectNames(i))
B(i).Events = Events
B(i).Events(i2,2) = {zeros(1,100)}

end

end

% add delays to struct

for i = 1:length(SubjectNames)
for i2 = 1:length(Events)

b = (ismember(t.Subject, B(i).Subject)) & (ismember(t.Event, B(i).Events{i2})) 
idx = find(b) 
B(i).Events(i2,2) = {t.Delay(idx)}
   
  end

end

% add delays to events in Brainstorm

for i = 1:length(RawFiles)

    sRaw = in_bst_data(RawFiles(i).FileName, 'F');

for i2 = 1:length(B)

if contains(string(sRaw.F.filename),B(i2).Subject)

sRaw.F.events(iEvtNew11).times(1,:) = sRaw.F.events(iEvtNew11).times(1,:) + B(i2).Events{1, 2}'
  
  end   
   end

sRaw.F.events(iEvtNew11).times = ...
round(sRaw.F.events(iEvtNew11).times .* sRaw.F.prop.sfreq) ./ sRaw.F.prop.sfreq;

bst_save(file_fullpath(RawFiles(i).FileName), sRaw, 'v6', 1); % saves modifications on data file in Brainstorm database

end