11.5: Exercise- The Matlab Command Line and the EEG Variable
-
- Last updated
- Save as PDF
A Matlab script is simply a series of Matlab commands that are stored in a text file. Running a script is equivalent to typing the commands in the Matlab command line. So, we’re going to start by running some commands from the command line.
To start, quit EEGLAB if it’s already running. Then, type clear all on the Matlab command line (i.e., at the prompt in the Matlab Command Window pane). This clears everything out of Matlab’s memory, which is a good thing to do when you’re first getting started on a new task. When you clear the variables, anything that was in Matlab’s Workspace pane should disappear. Another nice housekeeping command is clc , which clears the command window so that you’re not distracted by what happened earlier.
By the way, Matlab doesn’t do anything with a command until you hit the Return key (which may instead be labeled Enter on your keyboard). When I say that you should type something on the Matlab command line, you should follow it with Return or Enter . If you didn’t already press Return/Enter after clear all , do it now.
Now launch EEGLAB by typing eeglab on the command line, set the Chapter_11 folder to be the current folder, and load the dataset named 1_N170.set into EEGLAB. You should now see a set of variables in the Workspace pane, as shown in Screenshot 11.1. This includes EEG , which EEGLAB uses to store the current dataset, and ALLEEG , which EEGLAB uses to store all of the datasets that are available in memory. ERPLAB also creates corresponding ERP and ALLERP variables to hold the current ERPset and all available ERPsets.
You can see the contents of a variable by typing its name on the command line. Let’s try it! Type EEG on the command line (followed by the Return key, of course). Variable names in Matlab are case-sensitive, so make sure you type EEG and not eeg or Eeg . Once you type this, the contents of the EEG variable will be shown in the Command Window. Screenshot 11.2 shows the first few lines.
EEG is a complicated variable that contains many individual fields (you can learn about the details by typing help eeg_checkset on the command line). For example, the field named EEG.setname stores the name of the dataset, which is shown in EEGLAB > Datasets . Let’s change the name of the dataset. To do this, type EEG.setname = 'My First Custom Dataset' . (Note that the period at the end of the sentence is not part of the command you should type. I use boldface to indicate the exact text you should type.) Matlab will then print out the whole EEG variable again in the Command Window, and you’ll be able to see that the name has changed.
Here are a couple important things to note about the command you just entered:
- Matlab uses single quote marks to indicate literal text. If you didn’t use the quote marks and had instead typed EEG.setname = My First Custom Dataset , Matlab would have assumed that My First Custom Dataset was a sequence of four variable names ( My , First , Custom , and Dataset ). See the text box below for a hint about single quote marks.
- Most Matlab commands return one or more variables, and the value of the returned variables is ordinarily printed in the Command Window. You can suppress this by placing a semicolon at the end of the command. To see this in action, type x = 1 (followed by the Return key), and then type x = 2; (again followed by the Return key).
- When you change the set name using the command line, you won’t see the new set name in the Datasets menu. The reason for this will be explained later in this section.
You can also see the contents of a variable by double-clicking on the name of the variable in Matlab’s Workspace pane. Try double-clicking the EEG variable in this pane. A new Variables pane should appear in Matlab, showing you the fields of the EEG variable. One of those fields is named times , and it contains the latency in milliseconds of each time point in the dataset. Double-click it to see its contents; a new tab will open labeled EEG.times , and you’ll see a very wide list of latency values. The EEG was sampled at 250 Hz, so the first point is 0 ms, the second point is 4 ms, the third point is 8 ms, etc.
Single Quotes
Click on the tab for the EEG structure and take another look at the times field. Next to the times name, you should see 1 x 170750 double . The term double is used by Matlab (and many other programming languages) to refer to a number that is stored in scientific notation (e.g., X times 10 Y ) using double the ordinary precision (and therefore double the amount of storage space). The 1 x 170750 part indicates that EEG.times is an array of these double-precision numbers with 1 row and 170750 columns. If you go back to the tab for EEG.times , you’ll see that it has one row and 170750 columns (one column for each data point in the dataset).
In the tab showing the EEG variable, you’ll see a variable named data , which is listed as 33 x 170750 double . This variable stores the actual voltages in the dataset. It has 33 rows (one for each channel) and 170750 columns (one for each time point). That’s a pretty natural way to store EEG data, isn’t it?
When you start writing scripts, it’s easy to get confused about the rows versus the columns of an array. I find it helpful to look at the array in the Variables pane to remind myself which dimension is the rows and which is the columns.
Click on the tab for the EEG structure and take another look at the times field. Next to the times name, you should see 1 x 170750 double . The term double is used by Matlab (and many other programming languages) to refer to a number that is stored in scientific notation (e.g., X times 10 Y ) using double the ordinary precision (and therefore double the amount of storage space). The 1 x 170750 part indicates that EEG.times is an array of these double-precision numbers with 1 row and 170750 columns. If you go back to the tab for EEG.times , you’ll see that it has one row and 170750 columns (one column for each data point in the dataset).
In the tab showing the EEG variable, you’ll see a variable named data , which is listed as 33 x 170750 double . This variable stores the actual voltages in the dataset. It has 33 rows (one for each channel) and 170750 columns (one for each time point). That’s a pretty natural way to store EEG data, isn’t it?
When you start writing scripts, it’s easy to get confused about the rows versus the columns of an array. I find it helpful to look at the array in the Variables pane to remind myself which dimension is the rows and which is the columns.