11.8: Exercise- From the Command Line to a Script
-
- Last updated
- Save as PDF
In this exercise, we’ll see how commands that you type on the command line can be run from a script file. To start from a blank slate, you should quit EEGLAB, type clear all on the command line, and relaunch EEGLAB. Make sure that Chapter_11 is the current folder in Matlab, and open the file named Script1.m by double-clicking the name from Matlab’s Current Folder pane. This script consists of three lines, which open a dataset, add it to ALLEEG , and redraw the Matlab GUI.
Copy the first line of the file into your computer’s clipboard, and then paste it onto the command line to execute the command. You can determine whether it has successfully loaded the dataset into the EEG variable by looking at this variable in the Matlab Workspace pane. Now paste the second line of the file onto the command line to add the dataset to ALLEEG (which you can verify by looking at ALLEEG in the Workspace pane). Now paste the third line to the command line to update the GUI. You can see that the dataset is now in the Datasets menu.
Let’s take a closer look at the first line: EEG = pop_loadset('filename','1_N170.set'); This is just like the first line of the EEG.history in the previous exercise, except that it doesn’t specify the path. That’s because the 1_N170.set file is in the current folder (which you can see in Matlab’s Current Folder pane). When you open a file but don’t specify a path, Matlab will first look in the current folder. If it doesn’t find the file there, it will search the entire Matlab path. When you save a file but don’t specify a path, Matlab will save it in the current folder. In the next exercise, we’ll look at a robust way to specify the path.
First, though, we’ll see how to run an actual script. In theory, you could just put a set of commands in any text file and then copy-and-paste them onto the command line. The file would just be a way to store the commands so that you can easily reuse them. In fact, I sometimes use script files for this purpose. But there are easier ways to execute the commands in a script file. The simplest is just to click the Run button in the Matlab script editor. In my version of Matlab, it’s a green triangle (see Screenshot 11.3). To see this in action, quit EEGLAB, type clear all , restart EEGLAB, and click the Run button in the editor window for Script1.m . If you look at the EEG variable, the ALLEEG variable, and the Datasets menu, you can confirm that each line of the script has executed. It’s just as if you had copied all three lines into the clipboard and pasted them onto the command line (but faster and easier).
Now let’s look at three ways that we can execute a portion of a script rather than the whole thing. The first is simple: Copy the set of lines that you’d like to execute into the clipboard and paste them onto the command line. You’ve already done this with single lines of code, but you can do this with multiple lines.
The other two ways are easier to demonstrate with a script that has several distinct parts, so load the script named Script2.m (e.g., by double-clicking on this name in the Current Folder pane). This script loads the data from Subjects 1 and 2. I’ll explain how this script works in the next exercise. For now, we’ll just see how to execute parts of it.
Start by quitting EEGLAB, typing clear all , and restarting EEGLAB. Now, go into the text editor for Script2.m , and select the first 7 lines (by dragging your mouse over those lines; note that you can see the line numbers along the left side of the window). Now right-click (or control-click) on the selected text, and select Evaluate Selection from the menu that pops up. (You can also see a keyboard equivalent, which will be more convenient for using this approach in the future.) You’ll be able to see that the dataset from Subject 1 has been loaded (e.g., in the ALLEEG variable), but we haven’t yet updated the EEGLAB GUI. To do that, select the last line of the script (line 12, eeglab redraw ) and execute it (by selecting it, right-clicking, and selecting Evaluate Selection from the popup menu). Now the dataset should appear in the Datasets menu.
Now select and execute lines 9-12 to load the dataset for Subject 2 and update the GUI. You should now see the datasets from both Subject 1 and Subject 2 in the Datasets menu. Note that you didn’t execute the lines of the script in order. You executed line 12 after lines 1-7 and then again after lines 9-11. This approach gives you a lot of flexibility. The lines in a script usually need to go in a particular order to give you the desired result. For example, you can’t filter a file until after you’ve loaded it. But sometimes you’re just using the script as a convenient place to store a bunch of commands that you might execute in some other order.
Now we’re going to look at one more way to execute commands from a script, in which we divide the script into sections and run one section at a time. If you look at Script2.m , you’ll see that some lines begin with a % symbol. This symbol indicates that the line is just a comment, not code that will be executed. You can also put a % symbol after a command to provide a comment about that command.
You should also note that some of the lines begin with two consecutive % symbols. This indicates the start of a new section . If you click anywhere within a section, the background color of that section changes. Screenshot 11.4 shows what it looks like when I click in the last section of the script. Once you’ve highlighted a section in this way, you can run that section of code by clicking the Run Section button in the tool bar near the top of the script editor window.