1.9: Understanding the Matlab Path
-
- Last updated
- Save as PDF
Now that you’ve had a chance to play around with EEGLAB and ERPLAB a bit, I want to discuss a vitally important concept, the PATH . Many of the problems that Matlab beginners encounter are a result of not understanding and appropriately setting the PATH. If you’re already quite familiar with the Matlab PATH, you can skip this section.
Simply put, the Matlab’s PATH defines where it will look for code and other files. When you type eeglab into the Matlab command window to launch EEGLAB, the only reason that Matlab knows where to find the EEGLAB code on your computer is that you (or someone) set the PATH to include the location of the files containing the EEGLAB code. And if you’re reading this section because you’re having a problem getting EEGLAB to run, there’s a good chance that you’re having a PATH problem.
As you know, your computer’s file system is divided into a set of folders (also called directories ). The folders are hierarchically organized: You have a main folder for each drive, and those folders contain sub-folders, which contain sub-sub-folders, etc. The location of a file can be expressed concisely as something like D:\books\ERP_Analysis_Book\Exercises\Chapter_2\1_N400_erp.erp (on a Windows system) or /Users/luck/Documents/books/ERP_Analysis_Book/Exercises/Chapter_2/1_N400_erp.erp (on a Mac or Linux system). To make life complicated, DOS and Windows use backslashes to separate the names of the folders, whereas Mac and Linux use forward slashes.
The location of a single file, when expressed this way, is the path to the file. But note that I’m using lower case to refer to the path for a single file. I use upper case to refer to Matlab’s PATH, which consists of a list of multiple paths. This list defines where Matlab will look for code and other files. But note: Matlab will first look in the current folder (indicated by the blue arrow in Figure 1.2) before looking in the folders defined by the PATH.
Let’s take a look at the PATH on your computer. In the main Matlab window, click on the icon labeled Set Path (circled in red Figure 1.2, but note that it might be somewhere else in your version of Matlab). This should cause a new window appear, like the one shown at the bottom of Figure 1.2.
You should see the main folder for EEGLAB in the PATH. If you recently installed EEGLAB, it should be at the top. If you’re having trouble getting EEGLAB to launch, make sure you have the EEGLAB path in your PATH. To add the EEGLAB folder to your PATH, click on Add Folder… , navigate to the location of the EEGLAB folder that you downloaded when you installed EEGLAB, and add it to the PATH. It’s also possible that the path is incorrect (e.g., because the folder was moved after the PATH was set). If you already have EEGLAB in your PATH but you don’t know how to verify that the path is correct, you can delete the existing EEGLAB path (including all the subfolders) using the Remove button and then add the path again using Add Folder…
When you’re done updating the PATH, click Save and then Close . If you click the Close button without saving, the updated PATH will work, but it will be forgotten when you quit from Matlab. You need to click Save before you click Close if you want Matlab to remember the path when you launch it again in the future.
Now let’s talk in a little more detail about how the PATH works. A complicated package like EEGLAB or ERPLAB is divided into tons of individual files (often called .m files because the filename ends in .m ). When you type eeglab in the Matlab command window, Matlab searches the path for a file named matlab.m , and then it executes the code in this file. The code in the eeglab.m file calls many other functions, such as eeg_checkset() . The code for a given function is typically stored in a separate .m file. When Matlab needs to call eeg_checkset() , it therefore looks for it in a file named eeg_checkset.m . Where does Matlab look to find this file? In the folders defined by the PATH, of course. The eeg_checkset.m file is actually located in a subfolder within the main EEGLAB folder. This is why there are actually many EEGLAB subfolders in your PATH.
When you add a new folder to the PATH, you would ordinarily click on Add with Subfolders… to make sure that all the subfolders within the new folder are in your path. However, EEGLAB is smart enough that it will automatically add the subfolders when you click Add Folder…
Here’s the key takeaway from what we’ve discussed so far: Almost every command and function in Matlab is stored in a .m file, and when you try to execute a command or function, Matlab searches the PATH to find it. If Matlab tells you that it can’t find a function or a .m file, this almost always means that you don’t have the PATH set correctly.
Another problem that can arise is that there might be two different .m files with the same name in your path. For example, if you install a new version of EEGLAB on your computer, and you add the folder containing this version to your PATH without removing the old path, Matlab might execute the wrong version. So, make sure to clean out the old folder (and any subfolders) using the Remove button when installing a new version.
It's also possible that you have another package (e.g., FieldTrip or PsychToolbox ) in your PATH that defines a function with the same name as one of the EEGLAB functions. For example, both EEGLAB and FieldTrip could have a function named PlotEEG Waveforms , which would be stored in a file named PlotEEGWaveforms.m . You would then have two identical files in your PATH. Which file will Matlab use when it tries to call the PlotEEGWaveforms function? The answer is simple: It searches the folder in the order that they’re listed in the PATH, and it stops searching once it finds a match.
Yet another common problem is that the path to a folder may have one or more spaces, such as D:\books\ERP Analysis Book\Exercises . That’s usually not a problem, but sometimes Matlab will interpret such a path as being three separate things ( D:\books\ERP and Analysis and Book\Exercises ). If this happens, you can usually solve the problem either by using underscores instead of spaces or by enclosing the path in single quotes (e.g., 'D:\books\ERP Analysis Book\Exercises' ).
If you want more information, Matlab’s online documentation provides a detailed description of how the PATH works .