Mark segment as bad with script

Dear Community,

I am trying to automatically mark segments as bad for all my subjects raw EEG data, from the beginning of the signal to the first trigger event, minus 100ms.
The first trigger event is different in time for every subject.

I have seen this post:

But as I mentioned before, in my case the bad segment would be different for every subject.

Is there a scripting solution for this?

Thank you


Nothing in the existing processes would allow you to create a new "BAD" event based only on the first marker. You could make work something out with a combination of multiple processes used a bit away from their initial goal:

  • Group by name: to duplicate an existing category into an new event category that includes the string "BAD"
  • Detect multiple responses, with a very long "minimum delay between events" and "keep only the first even": In order to keep only the first event of the duplicated category
  • Convert to extended event with a time window that includes all the beginning of the file, until the baseline of the first stim (eg. [-100, -0.500]s)

Otherwise, it might be simpler to write a Matlab script edit directly the event structure:

1 Like

Hello François,

thank you for your answer, it led me to a scripting solution that I share below.
It creates two bad segments:

  • one between the beginning and [the first trigger - 0.0502 s]
  • one between [the last trigger + 0.3002 s] and the end of the signal.

%% Beginning of script

timestep = 1/4800; % ~0.0002s | Sampling freq = 4800Hz
sLink = in_bst_data(sImpRawFile.FileName, 'F'); % Creates "Link To Raw File" structure, that contains meta-data
iBadEvent = length( + 1; = 'BAD'; = [1 0 0]; = [1 1]; % 2 segments = 1; = cell(1,2); = cell(1,2); = [0;... sLink.F.prop.times(2)];
bst_save(file_fullpath(sImpRawFile.FileName), sLink, 'v6', 1); % saves modifications on data file in Brainstorm database

%% End of script