Matlab Paths

Matlab Paths

This page looks specifically at how to read and modify your search path. For the most basic of basics, check out MatlabBasics. As well, MatlabProgramming talks about hw to read m-files and program with them, and MatlabDebugging touches specifically on strategies for troubleshooting - how to get more information, what parts of error messages to look for, and how you can dig through a script to find what's gone wrong.

In the following, text in fixed-width font are commands that you can type directly into Matlab.


Every time you type a name in Matlab - of a variable, a function, a script, anything - the program goes through a certain procedure to figure out what you're referring to. It'll first look in its workspace for variables of that name; if it doesn't find anything, it will assume you're trying to refer to a function, script, or .mat file. Matlab always looks first in the present working directory for the .m file or .mat file of the given name, but if it doesn't find one there, it has a specified sequence of directories it looks in.

This directory list is called the path. It's similar to the Linux concept of the same name. Maintaining and manipulating your path can be crucial to running Matlab programs and keeping track of different versions of functions. Fortunately, Matlab makes it pretty easy to work with paths.

You can always look at your path in Matlab by typing path; Matlab searches the output list from top to bottom. The easiest way to work with your path is by typing "pathtool" at the Matlab prompt. That'll open up a graphical interface that will display your current path, let you easily add or remove directories, or re-order what's in there already.

If you're curious where on your path a particular m-file is, you can type which filename; this will tell you the location of the first copy of that file Matlab hits on the path, and hence which version of the file it will run. If you type open filename and filename is on your path, open will open the first copy it hits on the path. These two commands can be super helpful in figuring out whether you've got the right version of code, as well as figuring out where functions are located.

Matlab has a whole bunch of directories it adds into the path when it starts up by default, and the Gablab-specific commands to start Matlab - spm99-6-devel, spm2, spm2-devel, etc. - are essentially distinguished only by what directories they add to the path. If you call spm99-6-devel, the only difference from calling spm99-6 is that you'll get the spm99/devel directory on the top of your path above the standard spm99 directory. This means Matlab will find the development code first when it searches for functions, and it'll run the most current version of SPM that's stored in that directory. spm2-devel and spm99-6-devel differ only because they add different spm version directories.

In general, any manipulations of the path you do are good only for your current session of Matlab; when you shut it down and restart it, you'll go back to your default path. The default path in Matlab is stored in a file called pathdef.m, and in the Gablab, you won't be able to change that, which means clicking "save" in pathtool won't do much for you. But you can change your own default path! All you need is a startup.m file.

  • Startup.m files

    When you launch Matlab with any of the Gablab specific commands (spm99, spm99-6, spm99-6-devel, spm2, spm2-devel), Matlab will automatically search for an m-file called startup.m in a subdirectory of your home directory. If you're launching SPM99, it will search in /matlab/spm99; if you're launching SPM2, it will search in /matlab/spm2. If it doesn't find a startup.m file there, no biggie - nothing happens. If a file called startup.m exists there, though, it will automatically run that file. This can be a super useful way for you to customize Matlab for your own use. There are a number of commands you can run in Matlab to change output or parameters, but one obvious thing to do is customize your path.

    The command addpath('directoryname'); in a startup.m file will automatically ensure that directoryname is the first directory on your path whenever you start Matlab. If you have several addpath statements in there, they'll run in order, adding to the top each time, so the last addpath statement specifies the first directory on your path. This is really helpful if you want to keep your own Matlab code (or Matlab data) available above anything else. Simply add the directory where you keep the code or data to the top of your path, and you'll always be able to run it, no matter where your present working directory is. You can separate out your SPM99 code and data from your SPM2 code and data by using two separate startup.m files; Matlab will only look in the appropriate /matlab subdirectory, so your SPM2 paths can be kept in your spm2 startup.m file and so on.