Skip to main content
Social Sci LibreTexts

2.12: A Simple Matlab Script

  • Page ID
    108229
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    How long did it take you to run all of the steps in this chapter? Quite a while, I suspect. Now imagine that there are twice as many steps (as will typically be the case), and that one of them takes 20 minutes to run (which is typical for the artifact correction process that we typically use). And imagine that you’ve collected data from 40 participants and need to repeat the sequence of steps for each of them. Now imagine that a reviewer insists that you change one of the steps, so now you need to run them all again. Does this sound like fun? I didn’t think so. Also, when you do each step “by hand” using the EEGLAB/ERPLAB GUI, you’re likely to make occasional errors.

    To make your life easier and to reduce errors, you can automate the EEGLAB and ERPLAB processing steps using a Matlab script. A script is basically a set of commands that you could type in the Matlab command window but that you inside save as a text file with a .m extension (sometimes called a .m file). To make things more efficient, a script can include more sophisticated programming elements like loops, if/then statements, and variables. If you already know how to program in a typical programming language, you’ll find these aspects of Matlab scripting to be pretty straightforward. If you don’t already have much programming background, this book will teach you the basics.

    Fortunately, EEGLAB and ERPLAB have a history feature that makes scripting a lot easier. Every time you do something in the EEGLAB/ERPLAB GUI, the equivalent script command is saved in the history. For example, when you ran the artifact detection step, the following was saved in EEGLAB’s history:

    pop_artextval( EEG , 'Channel',1:30, 'Flag',1, 'Threshold', [ -100 100], 'Twindow', [ -200 795] );

    This makes it really easy to see how to create a line of script that corresponds to a step you carried out in the GUI. In fact, when I write EEGLAB/ERPLAB scripts, I often do the steps first in the GUI, look at the history, and then copy the relevant commands from the history into my .m file. In a later chapter, we’ll discuss how to access the history and build your own scripts. For now, let’s run an example script just so that you can see the basic idea.

    In the folder for this chapter, there is a file named preprocess_EEG.m. You should be able to see it in the Current Folder pane of the Matlab GUI. Double-click the filename to open it in the Matlab text editor. This text editor recognizes certain aspects of the Matlab scripting syntax and uses different coloring for different parts of the file (even though the actual file is just plain text without any colors specified, as you can verify by opening it in some other text editor). Lines that begin with a percent symbol are comments, and by default they’re colored in green. I added some comments at the top of the script that explain how to run it, and I also put a comment above each line of code to explain what that line does. For example, near the bottom you will see the pop_artextval command for doing artifact detection, and there is a comment above that line that explains what it does.

    This script does all the basic EEG preprocessing steps that we covered in this chapter. Read through the comments in the script file to see the steps. The last step prior to averaging was artifact detection, and the dataset created by this step is saved to a file. The filename has _from_script appended to it.

    To run the script, first quit EEGLAB to make sure that we don’t have any conflicts. Then, run the script. There are several ways to do this, including a Run button in the tool bar at the top of the Matlab text editor window for the preprocess_EEG.m file. With some luck, you’ll see a bunch of text appear in the Matlab command window, with information about each step that is running. When everything is done, you’ll see a prompt in the command window.

    If you see an error message or have some other kind of problem, then you’re about to learn some something useful! Start by reading the error message and trying to figure out how to solve the problem. If that doesn’t work, see if there is someone around who has experience running Matlab scripts and can give you some help. And if you can’t find someone to help, it’s time to consult the troubleshooting tips in Appendix A2. You might also want to read the first half of Chapter 11 to learn some of the basic concepts of EEGLAB/ERPLAB scripting.

    Once you’ve gotten the script to run, you should see that it has created a new dataset file named 6_N400_preprocessed_filt_elist_bins_be_ar_from_script in the same folder with all the other files from this chapter. Launch EEGLAB and open this file using EEGLAB > File > Load existing dataset. Take a look at the EEG using EEGLAB > Plot > Channel data (scroll). It should look just like the EEG data that you saw after going through all the preprocessing steps with the GUI.

    I hope you can now see how much faster it is to process a participant’s data with a script than by doing it with the GUI. However, this doesn’t mean that you won’t need the GUI once you’ve learned EEGLAB/ERPLAB scripting. You should use the GUI the first time you go through a participant’s data so that you can check for problems and errors. You’ll also need to use the GUI to determine the appropriate artifact detection parameters (and artifact correction parameters once you learn about that). But once you’ve processed the participant’s data manually and determined the right parameters, you can run a script using those parameters to reprocess the participant’s data. That way you can avoid any errors you might make when you process the data manually. And when Reviewer 2 asks you to change your preprocessing pipeline, you’ll be able to reprocess all the data quite easily and won’t be as grumpy about making the change.


    This page titled 2.12: A Simple Matlab Script is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Steven J Luck directly on the LibreTexts platform.

    • Was this article helpful?