top of page

Writing EEGLAB Scripts

A script is a set of steps (functions) that are all called at once, and can be run on a batch of files. Writing scripts will save you hours, and will do repetitive tasks for you. Also, you can run multiple slow steps (such as ICA) back-to-back overnight.

For more info and another tutorial, here is the EEGLAB script writing tutorial.


Steps:


Create a New Script & Add a Header

Create a new Script. Click New → Script OR type Command N.

Add an appropriate header.


Copy the Framework

Copy this framework and paste it into your own script, after your header. Copy and paste everything between the yellow lines. Use this framework anytime you want to be able to select one or more files to run a process on.


Example Code: 

clear all

close all

 

%-----------------------These are the things you need to specify---------------------------------------------------------

 

% PUT THE THINGS THAT WILL BE THE SAME FOR EVERY FILE HERE. FOR EXAMPLE: A SAVE FOLDER OR THE LOCATION OF THE CHANNEL LOCATIONS FILE.

% EXAMPLE: 

% exampleFolder = ('/Users/eeglab/Desktop/Post ICA Files 2/');   % Folder where finished files will be saved.

% 

% CLpath = '/Users/eeglab/Documents/MATLAB/GSN256.sfp';               % The full path of the file containing the channel locations

 

% DEFINE YOUR VARIABLES HERE: ===========================================================================================

 

 

 

%========================================================================================================================

%------------------------------------------------------------------------------------------------------------------------

 

%% Ask for .raw file

% This function opens up a popup menu where you can click the files you

% want to run your function on. If you want to select multiple files, hold

% down command or shift while clicking. 

 

% Get the files from a popup menu

% Use this function as [fileName, pathName] = uigetfile(filetype, title,

% defaultFolder, 'MultiSelect','on')

%

% filetype:      * followed by the extention for the filetype you want, all in single quotes. Example: '*.raw' or '*.mat'

% title:         the title of the popup window. You can put instructions to the user here, such as 'Select the .raw file(s).'

% defaultFolder: the path to the folder the menu opens up with. You can click to other folders from there. The path

%                needs to be in single quotes. Example: '/Users/eeglab/Desktop/First Draft Data/After Comp Removal'

[fileName, pathName] = uigetfile('*.set', 'Select the file(s)', '/Users/eeglab/Desktop/First Draft Data/After Comp Removal', ...

    'MultiSelect', 'on');

 

length = size(fileName);

 

if (iscellstr(fileName) == 0) 

    iMax = 1;

else

    iMax = length(1,2);

end

 

for i = 1:iMax

    if (iscellstr(fileName) == 0) 

        dataPath = strcat(pathName, fileName);

    else

        dataPath = strcat(pathName, char(fileName(1,i)));

    end

    

    % Parse the file path

    [pathName,name,ext] = fileparts(dataPath);   % pathstr = String containing the part of filename interpreted as a path name

                                                 % name = String containing the name of the file without any extension

                                                 % ext = String containing the file extension only, beginning with a period (.)

 

    fileNameWithExt = strcat(name,ext);          % Combine file's name and extention into one string called fileNameWithExt

                                                 

    % PUT YOUR CODE HERE: ===================================================================================================

 

    

 

    %========================================================================================================================

end 

This code is from framework.m. Your code goes between the sets of rows of equals signs.


Do the process with EEGLAB menus

(aka Graphic User Interface) If you’re using EEGLAB functions, this step is a bit easier because EEGLAB will do some of the work for you.


Open (or close and reopen) eeglab. If you don’t restart eeglab, you’ll have extra functions show up in your history script.


Load or import a dataset.


To import a dataset, follow these instructions.

To load a dataset, in the EEGLAB window click File → Load existing dataset


Do the process you want your function to do using the graphic interface (menus). For more info on these functions, see the other sections of this website, or the EEGLAB tutorials website. Carefully set the parameters in the menus. Most popup menus have the name of the function in the title bar:

For popup menus like the first two above (pop_eegfiltnew and pop_topoplot), the parameters you type in the boxes  (inside the blue boxes) will show up correctly formatted as inputs to these functions in the history script. This’ll make more sense when we get to the history script section. 

Save the dataset. Click File → Save current dataset as


History Script

Now save the session history script.

Click File → History scripts → Save session history script

Add a brief description to the file name. Note that the history script is saved to the MATLAB folder.

Now open the history file you just saved (from the MATLAB folder, which can be accessed in the left sidebar)

Copy and paste it into the framework in your script in the “PUT YOUR CODE HERE ” section (almost at the bottom of the script).

Also, add this line of code right before the code you just pasted. This code opens EEGLAB.

[ALLEEG EEG CURRENTSET ALLCOM] = eeglab;

Now add comments that describe what the functions are accomplishing. The functions tend to have somewhat descriptive names, for example, pop_loadset loads the dataset, and pop_epoch epochs the data. Most of the EEGLAB functions have “eeg_” or “pop_” at the beginning to designate that they are EEGLAB functions. If you’re not sure what a function is doing, look it up in the documentation by clicking here. The eeg_checkset function makes sure the dataset is ok. Leave those where they are, but you don’t need to specifically comment them.


Replace filenames with Variables

You now need to replace some of the filenames with variables so that you are processing the correct file and naming it correctly.

For file names & paths that refer to the file you selected/loaded/opened:

For file names & paths that refer to the name and place you saved the file

(after the processing)

Replace the path name with a variable such as saveFolder, and define this variable in the “Define your variables here” section.

For the example in the video, I replace ‘/Users/eeglab/Desktop/’ with saveFolder, and then I define saveFolder as ‘/Users/eeglab/Desktop/’ in the DEFINE YOUR VARIABLES HERE section:

Replace the filename with strcat(name, ‘_yourTagHere.ext’) where:

‘_yourTagHere’ is a brief description of what was done to the file. For example: ‘_ICA’ or ‘_epoched’.

.ext is the extension that the file will be saved with. For example: .set or .mat

For dataset names

Anywhere the specific file name is, replace it with strcat(name, and the other parenthesis where appropriate.

For example: ‘001_B Epoched’ → strcat(name, ‘ Epoched’).

Related Posts

See All

Automation Scripts

I’ve written a set of scripts that work data through our analysis pipeline. Each dataset will have its own set of scripts because every...

Other Info

BIOPAC 101 NetStation, EEGLAB, and FieldTrip often use different words to describe the same thing. When turning on the laptop, hold down...

Filename Key

Please update the keys as you create new abbreviations. AA_017_preQR_ICA_Af.set StudyCode_SubjectNumber_Condition_AnalysisPerformed_Stimu...

Video Tutorials
AcqKnowledge Tutorial - Part 2
06:48
AcqKnowledge Tutorial - Part 1
02:11
Meta-analysis in R - Plotting Data
07:47
Meta-analysis in R - Data Culmination
01:18
Meta-analysis in R - Filtering Data
01:04
bottom of page