matlabfor-loopeeglab

For loop for different datasets and conditions for EEGLAB


I have to analyze some EEG data and I am trying to automatize the pre-processing procedure.

I have 40 participants. Every participants has 4 different files for 4 conditions.

So the files are saved as

1-1  
1-2  
1-3  
1-4  
2-1  
2-2  
2-3  
2-4  
...  

up to 40-4

The files are from BioSemi (.bdf)

I have been able to automatize the pre-processing procedure, but every time I have to chose a different file, run the script, and save it.

I would like to run a for loop that does it all by itself (take 1-1, run the pre-processing, save, take 1-2...etc).

Following I am pasting what I've got so far.

I've been trying all day to write a for loop, but it is just not working.

I would really appreciate any help.

This is the current pre-processing script:


subject = '1-1.bdf'  

%Open EEGLAB and inizialize several EEGLAB variables (listed in the output
%function  
[ALLEEG EEG CURRENTSET ALLCOM] = eeglab;  

%Load a file  
%EEG=pop_loadset;  % pop up window to input arguments  

EEG = pop_biosig(subject)  %Reads in the dataset frin a BIOSEMI file  

% Stores the dataset into EEGLAB  
[ALLEEG EEG CURRENTSET ] = eeg_store(ALLEEG, EEG);  

%Change sampling rate to 512  
EEG = eeg_checkset( EEG );    
EEG = pop_resample( EEG, 512);    
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET); %save it as a new dataset  

% Edit Channel Location  
EEG = eeg_checkset( EEG );  
EEG=pop_chanedit(EEG, 'lookup','D:\\Matlab\\eeglab_current\\eeglab13_5_4b\\plugins\\dipfit2.3\\standard_BESA\\standard-10-5-cap385.elp');  
% Store the dataset into EEGLAB  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  

% Add some comments  
EEG.comments = pop_comments(EEG.comments,'','Sampling rate was changed to 512.',1);  
% You can see the comments stored with the dataset either by typing >> EEG.comments or selecting the menu option Edit->About this dataset.  

%Select Data. Remove EXG5:EXG8   
EEG = eeg_checkset( EEG );  
EEG = pop_select( EEG,'nochannel',{'EXG5' 'EXG6' 'EXG7' 'EXG8'});  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  


%HighPassFilter 0.5  
EEG = eeg_checkset( EEG );  
EEG = pop_eegfilt( EEG, 0.5, 0, [], [0], 0, 0, 'fir1', 0);  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  

%LowPassFilter 50  
EEG = eeg_checkset( EEG );  
EEG = pop_eegfilt( EEG, 0, 50, [], [0], 0, 0, 'fir1', 0);  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  


%ReReference to 35 36 (EXG3. EXG4)  
EEG = eeg_checkset( EEG );  
EEG = pop_reref( EEG, [35 36] );  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');   

% Reject Continuous By Eye  
EEG = eeg_checkset( EEG );  
pop_eegplot( EEG, 1, 0, 1);  

eeglab redraw % Update the EEGLAB window to view changes  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Automatic Channel Rejection  
EEG = eeg_checkset( EEG );  
EEG = pop_rejchan(EEG, 'elec',[1:34]   ,'threshold',5,'norm','on','measure','prob');  
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 2,'overwrite','on','gui','off');  

eeglab redraw % Update the EEGLAB window to view changes 

Solution

  • Here's how you can access the filenames in a loop, so that you can run your MATLAB script. The simplest thing to do is put your .bdf files in a folder by themselves. Then write a function that wraps the functionality you want, like this:

    function run_script_with_loop(pathname)
    
    file_struct_list = dir([pathname filesep() '*.bdf']);  %% get list of .bdf files in the pathname specified
    
    filename_list = {file_struct_list.name};  %% extract the filenames into a cellarray
    for subject = filename_list  %% this iterates over the elements of the cell array, one-by-one, setting the `filename` variable like a loop variable
        [ALLEEG EEG CURRENTSET ALLCOM] = eeglab;  
        full_pathname = [pathname filesep() subject{1}];
        EEG = pop_biosig(full_pathname);  %% perform your processing
        ...
    end
    

    A few comments: