Quick help with scripting

Hello BST team,
I know that asking for help with scripting is a bit outside the scope of this forum but I am stuck and ran out of ideas for troubleshooting so I am just trying my luck to see if anyone has any thoughts on this.
I am trying to run the following script for PSD analysis but when it gets to the second loop for the second ".mat" file I get the error message below that the line isFile = sFiles{i}; brace indexing is not supported. This was completely unexpected for me and I do not understand why this is the case. I've tried (), {}, and and none of them seem to solve the problem. What can I do to fix this?
image

sFiles = {...
    'Test_PSD/@raw5AD13_s3_sleep_epochs/data_0raw_5AD13_s3_sleep_epochs.mat'...
    'Test_PSD/@raw5AD13_s5_sleep_epochs/data_0raw_5AD13_s5_sleep_epochs.mat'};

for i = 1 : length(sFiles)
    isFile = sFiles{i};

Start = 0 : 4 : floor(in_bst(sFiles{i}).Time(end));
End = 4 : 4 : round(in_bst(sFiles{i}).Time(end));

for j = 1 : length(End)
    One = Start(j);
    Two = End(j);

% Process: Power spectrum density (Welch)
sFiles = bst_process('CallProcess', 'process_psd', isFile, [], ...
    'timewindow',  [One, Two], ...
    'win_length',  4, ...
    'win_overlap', 0, ...
    'units',       'physical', ...  % Physical: U2/Hz
    'sensortypes', 'MEG, EEG', ...
    'win_std',     0, ...
    'edit',        struct(...
         'Comment',         'Power,FreqBands', ...
         'TimeBands',       [], ...
         'Freqs',           {{'delta', '0.1, 4', 'mean'; 'theta', '5, 7', 'mean'; 'alpha', '8, 12', 'mean'; ...
         'sigma_avg', '11, 16', 'mean'; 'sigma_max', '11, 16', 'max'; 'beta', '13, 29', 'mean'; ...
         'gamma1', '30, 59', 'mean'; 'gamma2', '60, 90', 'mean'; 'BG_EEG1', '4, 10', 'mean'; 'BG_EEG2', '20, 30', 'mean'}}, ...
         'ClusterFuncTime', 'none', ...
         'Measure',         'power', ...
         'Output',          'all', ...
         'SaveKernel',      0));
end
end

There are a few issues that need to be addressed:

  1. Variable Overwrite: You're overwriting sFiles inside the loop. This will cause problems in subsequent iterations. You should use a different variable name for the output of bst_process.

  2. Indexing of sFiles: Inside the loop, you are using sFiles{i} but then overwrite isFile with this value. It's more direct to use sFiles{i} in your function call.

  3. Time Window Calculation: The calculation of Start and End might not align correctly if the time in in_bst(sFiles{i}).Time(end) is not a multiple of 4. You need to ensure the last window is handled correctly.

Here's a revised version of your code:

sFiles = {... 
    'Test_PSD/@raw5AD13_s3_sleep_epochs/data_0raw_5AD13_s3_sleep_epochs.mat',... 
    'Test_PSD/@raw5AD13_s5_sleep_epochs/data_0raw_5AD13_s5_sleep_epochs.mat'
};

for i = 1 : length(sFiles)
    fileData = in_bst(sFiles{i});
    Start = 0 : 4 : (floor(fileData.Time(end) / 4) * 4);
    End = 4 : 4 : (ceil(fileData.Time(end) / 4) * 4);

    for j = 1 : length(End)
        One = Start(j); 
        Two = End(j);

        % Process: Power spectrum density (Welch)
        processedFiles = bst_process('CallProcess', 'process_psd', sFiles{i}, [], ... 
            'timewindow', [One, Two], ... 
            'win_length', 4, ... 
            'win_overlap', 0, ... 
            'units', 'physical', ... % Physical: U2/Hz 
            'sensortypes', 'MEG, EEG', ... 
            'win_std', 0, ... 
            'edit', struct(... 
                'Comment', 'Power,FreqBands', ... 
                'TimeBands', [], ... 
                'Freqs', {{'delta', '0.1, 4', 'mean'; 'theta', '5, 7', 'mean'; 'alpha', '8, 12', 'mean'; ... 
                           'sigma_avg', '11, 16', 'mean'; 'sigma_max', '11, 16', 'max'; 'beta', '13, 29', 'mean'; ... 
                           'gamma1', '30, 59', 'mean'; 'gamma2', '60, 90', 'mean'; 'BG_EEG1', '4, 10', 'mean'; 'BG_EEG2', '20, 30', 'mean'}}, ... 
                'ClusterFuncTime', 'none', ... 
                'Measure', 'power', ... 
                'Output', 'all', ... 
                'SaveKernel', 0
            )
        ); 
    end 
end
1 Like

@Sylvain thank you very much for these explanations! appreciate it a lot :slight_smile: