How to script the exclusion of bad trials from an array of data files?

Hi,

I ran a home-made matlab analysis on some files sFiles that I get out from a Braintorm process, and after this analysis, I marked some of the files as bad trials using the SetTrialStatus function of process_detectbad. Now I would like to select the subset of sFiles that are not marked as bad. I considered using process_select_files_data, but this process does not take any input file, and I would need to start from sFiles preferably, to make sure I'm not including other irrelevant files. Is there a simple matlab code that could do what I need?

In short, I am looking for some code some_function that I could use as follow:
sFilesGood = some_function(sFiles);
The output sFilesGood would contain all entries of sFiles that are not marked as bad.

Any help with this would be greatly appreciated.

There is no process of function that can directly filter the good/bad trials. In the standard Brainstorm script workflow, the selection of the good trials is done during the selection of the files itself (the creation of the sFiles list, either from the Process1 box or from a selection process list process_select_files_data).

The easiest in terms of code writing would be to select again your files with a selection process, but I understand you'd like another solution to avoid select extra unwanted files.
Otherwise, you need to write a loop that gets the BadTrial information for each file. Something like:

isBad = false(length(sFiles),1);
for i = 1:length(sFiles)
    [sStudy, iStudy, iData] = bst_get('DataFile', sFiles{i});
    isBad(i) = sStudy.Data(iData).BadTrial;
end
sFiles(isBad) = [];

Thank you François!