Coding for **By trial group (folder average)**

Everything you've done so far looks good.

What I'm having trouble implementing is the [iGroups, GroupComments] part and the "for" loop with "i = 1:length(iGroups)" where and how do I implement this?

Your computation and the "SAVE THE RESULTS" block both go inside the for loop.
Instead of using sInputs, you must use sInputs(iGroups(i)), in order to select only the files within the input group at each iteration. Same goes for AllMat => AllMat(:, :, iGroups(i))

Alternatively, you could move the block that loads all the input files into AllMat inside the group for loop as well. This will be lighter in memory (not all the trials of all the subjects are loaded at once, only the ones that are averaged together), and probably easier to implement as you don't have to add any indices when accessing AllMat.

You need to be careful about the STUDY in which you save each average. Use GetOutputStudy, as in process_average.m, instead of using the path of the first input file, as in process_example_customavg (fileparts(sInputs(1).FileName))
[sStudy, iStudy, Comment, uniqueDataFile] = bst_process('GetOutputStudy', sProcess, sInputs(iGroups(i)));

In case you wonder how all these options work more precisely, this is all documented in this tutorial:
https://neuroimage.usc.edu/brainstorm/Tutorials/TutUserProcess