11.9: Exercise- Using a Variable for the Path
-
- Last updated
- Save as PDF
In this exercise, we’re going to take a closer look at Script2.m , which demonstrates how to use variables to specify the paths to your files, which is much more robust. Make sure that Chapter_11 is still the current folder, and type pwd (which stands for print working directory ) on the command line. You should see the path for your current folder. Line 3 of Script2.m uses the pwd command to get the path to the current folder and store it in a variable named DIR (short for directory , but we could have named it almost anything). Run line 3 of the script (using any of the methods described in the previous exercise) and then look at the value of DIR (by typing DIR on the command line or by double-clicking it in the Workspace pane). You’ll see that DIR is a string that holds that path to the current folder.
Now we can use DIR as the starting point for providing paths to our Matlab commands. This is much more robust than specifying something like C:/ERP_Analysis_Book/Ch_10_Scripting as the path. For example, if you move your data and scripts to a new location on your computer or a new computer, using DIR as the path will still work (because the pwd command will return the new location of the script), but specifying the path directly will now fail.
We didn’t really need to specify the paths in Script2.m , because the files were located in the current folder. Close Script2.m and open Script2b.m , which shows a more realistic example. This new script assumes a more complicated but better organization for your data files, in which each participant has a separate folder inside of a general data folder.
This is the organization that I generally recommend for EEG and ERP data (whether or not you use EEGLAB and ERPLAB). You will end up with a lot of files for each participant, and this just keeps them well organized. There’s some redundancy, because I also recommend including the Subject ID number in each filename. But this redundancy is useful because it minimizes the likelihood of errors. Nothing about EEGLAB or ERPLAB requires this organization, but it’s used for all the scripts in this book. You can choose a different organization, but please don’t just put all the files for all the participants into a single folder. It will seem simpler at first, but it will make your life much more difficult in the long run. You just don’t want to have 3478 files in the same folder!
This organization means that you need a different path for each participant. In Script2b.m , we implement this by using DIR as a base folder (the folder that contains the script) and then use this to create a variable named Subject_DIR that holds the path for the participant we’re currently processing.
Important note : This approach assumes that Matlab’s current folder is set to be the folder that contains the script. You should make sure this is true when you run the scripts in this book.
Line 6 of Script2b.m sets the value of Subject_DIR like this:
Subject_DIR = [DIR '/N170_Data/1/']; % Folder holding data for this subject
Putting multiple character strings inside of square brackets causes Matlab to concatenate the strings together. For example, ['ERPs' 'Are' 'Great'] is equivalent to ' ERPsAreGreat' , and ['ERPs ' 'are ' 'great'] is equivalent to ' ERPs are great' . If DIR has a value of ' C:/ERP_Analysis_Book/Ch_10_Scripting' , then [DIR '/N170_Data/1/'] has a value of ' C:/ERP_Analysis_Book/Ch_10_Scripting/N170_Data/1/' .
To see this in action, run lines 3 and 6 of Script2b.m and then look at the value of Subject_DIR . This is what it looks like on my computer:
/Users/luck/Dropbox/Research/Manuscripts/ERP_Analysis_Book/Ch_10_Scripting/Exercises/N170_Data/1/
This may seem like a lot of work, but I guarantee that it will make your life easier in the long run. For example, if you develop a script for one experiment and want to use it for a second experiment, you can place a copy of the script in the folder for the second experiment, and the script will automatically look for the data files in the correct place.
If you’re using a Windows machine, Script2b.m may not work properly because it uses slashes between folder names rather than backslashes. You can just replace the slashes in the paths with backslashes.