Using the Matlab Path Variable
What is the Matlab path? The Matlab path is a list of directories that tells Matlab where to find relevant m-files. Whenever you enter a command, Matlab searches the directories in the path until it comes across an m-file with the same name (plus the .m extension) and then executes that procedure. Basically, the path defines the universe in which Matlab is working at a given time.
A few technical notes: first, when you enter something in the command line, Matlab actually begins by looking for a variable of the given name, then for an m-file in the current directory, and only then does it enter the search path. You should never use variables and functions of the same name. Also, if the current directory contains an m-file that matches the command, Matlab executes that code and never enters the path. Finally, the type of m-file is irrelevant to the search procedure: Matlab executes the first match it finds, regardless of whether it contains a script, function or an object method.
Why does it matter? Proper path management is essential to ensure smooth running code. In order to use any code, the m-file containing it must be in one of the directories contained in the path. If you try to call a procedure that is stored outside of the path you will encounter errors. A bit more subtly, the order directories appear in the path can affect how your code functions. If you ever create two functions with the same definitions (called overloading), then Matlab will call whichever one that it encounters first. Most often users overload standard toolbox functions with slight modifications of their own. Overloading functions requires that you be very careful in defining your Matlab path.
How to define the path – standard methods. Contained somewhere in your Matlab user toolbox/local directory is a file called pathdef.m. This is where Matlab stores the default path definition. If you wish to write new code or use someone else’s, then you will need to modify this default path. The most direct way to modify your Matlab path definition is to use the path command in the Matlab command window. The following command appends /export/home/a1aaa00/rest/of/directory/name to your Matlab path:
path (path, ‘/export/home/a1aaa00/rest/of/directory/name:’)
Typing individual paths repeatedly can be quite cumbersome. Another approach is using the graphical interface by going to the File menu and selecting Set Path. You can use this to browse and select the directories you plan on using. By default, doing this only sets the path for your current Matlab session. As with the path command, your changes will be lost the next time you startup Matlab. You may want to save this path definition. If you have the necessary permissions, Matlab will save this in the aforementioned toolbox/local/pathdef.m file. This method redefines Matlab’s default path definition.
If you use a variety of packages and toolboxes, you may want to use different pathdefs at different times. Regardless of what default path you have selected, you may find yourself frequently modifying your paths. A possible solution is to save a given pathdef in a secondary pathdef.m file, somewhere outside of your local toolbox. Doing this allows to save more than one default path, but you must launch Matlab from the directory containing the pathdef.m file you whish to use. Otherwise, Matlab will look elsewhere for its path. This method has a significant drawback, however, if you happen to use multiple versions of matlab e.g. to run older code. Because they refer to version-specific toolboxes, pathdef.m files created with one version of Matlab may cause a different version to crash.
Another option is to create a startup.m file that sets your pathdef. A startup file is essentially a set of commands that you want Matlab to execute every time you begin a session. To do this, simply type the path commands (see above) you want executed in the startup.m file and you won’t have to type them again. This method adds to the default pathdef, instead of defining it, so you won’t run into the version inconsistency problems that we did above. That’s good, but it still forces you to start Matlab from the directory containing the proper startup.m file. Because they all have the same name, it can be difficult to recall where you’ve stashed a given startup.m and what path it declares.
How to define the path – superior methods. In UNIX/Linux-based systems, the best way to define your paths is using your .cshrc or .profile file. This is the file that control you UNIX preferences. You can use this file to select what directories to add automatically to your Matlab path whenever you launch Matlab. This method avoids both the version conflicts and the launch directory concerns mentioned before. To this, at the UNIX command line type
\sunserver% emacs .cshrc
This opens your .cshrc file. Don’t forget to include the period before cshrc. At the bottom the file add the following text, substituting in the directories you want to use.
setenv MATLABPATH /home/a1rxc03/matlab/local/paths:/home/a1rxc03/matlab/local/navigte:
Be sure to put everything on one line even though it is not here. You can enter as many directories as you want, always separating them by a colon. Here I’ve added two of my personal directories, paths and navigate. Save (crtl-x, crtl-s) and close (crtl-x, crtl-c) the .cshrc file. To activate the changes you’ve made to your preference at the prompt type:
/sunserver% source .cshrc
If Matlab is open, you will need to exit and relaunch for these changes to take effect.
The .cshrc method is adequate if you have a few packages and don’t need to change you’re pathdef that often. If, however, you think you want to change your pathdef frequently, there is one more step that you can take to best manage your path definitions. Somewhere in your Matlab directory –wherever you keep your m-files – create a subdirectory called paths. In this subdirectory, create scripts that set the pathdef to particular configurations. You might, for example, have a script that includes a certain set of statistical commands. Whenever you want to use those programs, call the script that includes the necessary directories. If I want to use only my own functions, I call a different script that only includes my directories. Now, you can create and save as many different Matlab path configurations as you want. You can also create commands that add certain directories without resetting whatever directories are already included in the pathdef. The final step is to include this directory in you .cshrc or .profile file. Now every time you open up Matlab one command is all you need to select the Matlab path that suits your needs for the moment.
As far as path management is concerned, that is as good as it gets.