Script: creating an if() statement to decide whether or not to force dipoles inside

Hello,

I currently have a script which automatically imports, sorts, and processes data to automatically compute the inverse solution.

For some of the subjects, all of the dipoles are within the BEM layer. For others, they are not and need to be forced inside. i.e., i get this warning: "WARNING: 4 dipole(s) outside the BEM layer "Brain".

Is there a way for me to implement an if() statement that basically does:

if (dipoles outside Brain BEM layer) {
force inside

else
proceed as normal }

I have searched the forums, and found this post that helped me automatically force dipoles inside the BEM layer, but I cannot find a post that specifcally can help me make an if() statement to check.

Thank you so much for your consideration to help. if more details are needed, please let me know and I will response fast.

Here is the function that computes the head model for a subject, then automatically forces dipoles inside the BEM layer. However, the problem is that some subjects dont require this step--which freezes the loop and I have to manually click to make the process continue.

function headFiles = esiHeadModel(subjectStruct,subNum,epFileMatrix,adaptive)

% subject ID label thing
subjectID_input = subjectStruct.subjectID(subNum);

% store names for epochs so brainstorm knows where to look. using a for
% loop makes it so that we can add more epochs down the line if desired
for i=1:length(epFileMatrix)
    headFileTemp = epFileMatrix(i).FileName;
    headFileNames{i} = char(headFileTemp);
end


    % Process: Compute head model
    headFiles = bst_process('CallProcess', 'process_headmodel', headFileNames, [], ...
        'Comment',     '', ...
        'sourcespace', 1, ...  % Cortex surface
        'meg',         4, ...  % OpenMEEG BEM
        'eeg',         3, ...  % OpenMEEG BEM
        'ecog',        2, ...  % OpenMEEG BEM
        'seeg',        2, ...  % OpenMEEG BEM
        'openmeeg',    struct(...
             'BemFiles',     {{}}, ...
             'BemNames',     {{'Scalp', 'Skull', 'Brain'}}, ...
             'BemCond',      [1, 0.0125, 1], ...
             'BemSelect',    [1, 1, 1], ...
             'isAdjoint',    0, ...
             'isAdaptative', adaptive, ...
             'isSplit',      0, ...
             'SplitLength',  4000), ...
        'channelfile', '');
    

    

        sSubject = bst_get('Subject', subjectID_input{1});
        TessFile = sSubject.Surface(sSubject.iCortex).FileName;
        EnvFile = sSubject.Surface(sSubject.iInnerSkull).FileName;
        tess_force_envelope(TessFile, EnvFile);


end
1 Like

Hi @willbanks, thank you for reaching out.

We updated tess_force_envelope to allow non-interactive calls. Commit: 0e6e0ba

So, now you can call it:

[NewTessFile, iSurface] = tess_force_envelope(TessFile, EnvFile, 0)

where the last argument (set to 0) indicates that the call is not interactive, so no figures are opened, and if there is no vertices to fix, nothing is done. Thus if all vertices are inside the innerskull, NewTessFile is the same as TessFile, and iSurface is the surface index for TessFile.

Update your Brainstorm instance to get this new feature:

2 Likes

thank you so much! I will give that a shot in a bit and let you know how it goes.

thanks again, I’m having a new error and was wondering if you knew where I could start to fix it

im getting this error

Insufficient number of outputs from right hand side of equal sign to satisfy assignment.

Error in esiHeadModel (line 37)
        TessFile = sSubject.Surface(sSubject.iCortex).FileName;

this error does not occur for the majority of subjects, only for certain ones. it is run from the function esiHeadModel

function headFiles = esiHeadModel(subjectStruct,subNum,epFileMatrix,adaptive)

% subject ID label thing
subjectID_input = subjectStruct.subjectID(subNum);

% store names for epochs so brainstorm knows where to look. using a for
% loop makes it so that we can add more epochs down the line if desired
for i=1:length(epFileMatrix)
    headFileTemp = epFileMatrix(i).FileName;
    headFileNames{i} = char(headFileTemp);
end


    % Process: Compute head model
    headFiles = bst_process('CallProcess', 'process_headmodel', headFileNames, [], ...
        'Comment',     '', ...
        'sourcespace', 1, ...  % Cortex surface
        'meg',         4, ...  % OpenMEEG BEM
        'eeg',         3, ...  % OpenMEEG BEM
        'ecog',        2, ...  % OpenMEEG BEM
        'seeg',        2, ...  % OpenMEEG BEM
        'openmeeg',    struct(...
             'BemFiles',     {{}}, ...
             'BemNames',     {{'Scalp', 'Skull', 'Brain'}}, ...
             'BemCond',      [1, 0.0125, 1], ...
             'BemSelect',    [1, 1, 1], ...
             'isAdjoint',    0, ...
             'isAdaptative', adaptive, ...
             'isSplit',      0, ...
             'SplitLength',  4000), ...
        'channelfile', '');
    

    

        % sSubject = bst_get('Subject', subjectID_input{1});
        % TessFile = sSubject.Surface(sSubject.iCortex).FileName;
        % EnvFile = sSubject.Surface(sSubject.iInnerSkull).FileName;
        % tess_force_envelope(TessFile, EnvFile, 0)


end

any idea what this could be? usually this error occurs in MATLAB when matlab was expecting to produce more outputs than it expected, i believe.

The error is clear regarding a discrepancy on the right in on assigning:
TessFile = sSubject.Surface(sSubject.iCortex).FileName;

The issue is likely on your script.

You can use Matlab's debugging tools to figure it out:

1 Like

thanks! I’ll let you know what comes of it. I appreciate your help as always.