Skip to main content
Social Sci LibreTexts

11.11: Exercise- Looping Through Data from Multiple Participants

  • Page ID
    137741
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    In this exercise, we’ll get to see how loops are used to load the datasets from a set of participants. We’ll add some processing steps that make the script more useful in the next exercise, but I wanted to keep things simple for now. To get started, quit EEGLAB, close any open scripts,  type clear all, and open Script4.m. But don’t launch EEGLAB—we’ll have the script do that!

    Go ahead and run Script4.m to see what it does. It should launch EEGLAB, load the datasets for Subjects 1-10 (except Subject 5), and refresh EEGLAB to make the datasets available in the Datasets menu.

    Now let’s look at the script and see how it works. The first line of code launches EEGLAB, which creates several variables that we will find useful (e.g., EEG and ALLEEG). The next line of code creates the DIR variable, as in the previous scripts, which holds the location of the script (and should be the Chapter_11 folder). Then the script creates a new variable named Data_DIR, which appends '/N170_Data/' onto the DIR variable. This gives us a path to the folder containing the single-participant data folders.

    The next step is to define a variable named Dataset_filename, which has a value of  '_N170.set'. We’ll eventually combine this variable with the subject ID to get the entire filename for a given participant (e.g., 1_N170.set).

    Then we define variables for the list of subjects and the number of subjects, just as in the previous example. Note that these steps embody the principle that all values used by a script should be defined as variables at the top of the script. It’s a little extra up-front work to do this, but it dramatically reduces the likelihood of bugs later (especially when you take a previous script and modify for a new purpose).

    The next step is to loop through the subjects. The first part of this is just like what we did in the previous script, including setting ID to be a string with the current subject’s ID. Then the script creates a variable named Subject_DIR, which specifies the folder that holds that data for the subject currently being processed by the loop (e.g., …/Chapter_11/N170/1/ for the first subject). We do this by concatenating the Data_DIR variable with the ID variable and then a / character. We also create a variable named Subject_filename by concatenating the ID variable with the Dataset_filename variable. This gives us a value of 1_N170.set for the first subject.

    We then load the dataset, using Subject_filename as the filename and Subject_DIR as the path. The dataset is stored in the EEG variable, and our last step in the body of the loop is to add this dataset to the ALLEEG variable using the eeg_store routine. The zero we specify as the last parameter for this routine tells it to add the new dataset to the end of ALLEEG.

    After the loop finishes, eeglab redraw is called to update the EEGLAB GUI.

    There are actually 40 participants in this experiment, each with a dataset. This script is a much faster way of loading these 40 datasets than using the GUI to separately load each one. Because all the key values are specified as variables at the top of the script, you can easily find them and modify them so that you can use the same script with another experiment, assuming that the data are organized in the same way on your computer. You’d just need to modify the list of subject IDs (the SUB variable), the name of the folder holding the data (Data_DIR), and the base dataset name (Dataset_filename). This will be much faster and easier if you’re consistent in how you organize the data for each experiment (see the text box below).

    Consistency

    There is a famous line from the poet Ralph Waldo Emerson that is frequently misquoted as “Consistency is the hobgoblin of little minds.” People sometimes use this incorrect version of the quote to belittle people for being consistent. However, the actual quote is “A foolish consistency is the hobgoblin of little minds” (Emerson, 1841 p. 14; my emphasis). It’s not the least bit foolish to be consistent about your data organization, your filenames, your variable names, etc. You will save yourself huge amounts of time and grief by developing a good organizational strategy early in your career and then sticking to it (but with thoughtful changes when necessary).


    This page titled 11.11: Exercise- Looping Through Data from Multiple Participants 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?