Managing Your Path in Matlab – Part II

In the last article I wrote about managing your path in Matlab, I covered some of the functions that deal with the search path, including path, matlabroot, addpath, rmpath and genpath.  These functions provide a solid base for viewing, adding and removing directories from your search path.  In this article, I will explain how to use several more functions that deal with your search path, including functions that make changes which persist after ending your Matlab session.

At the end of the first article, I mentioned that the Matlab search path is saved in a file called pathdef.m.  This file is saved in the directory matlabroot/toolbox/local by default. The function savepath allows the user to save the current search path to pathdef.m, which preserves the search path for future sessions.

The userpath function is another helpful function that allows us to add another path to the top of the search path.  Userpath can change the MATLABPATH environmental variable to a path of the user’s choice, which is always added to the beginning of the search path when Matlab starts up.

userpath  %returns the current userpath
userpath('newpath')  %sets userpath to newpath
userpath('reset')  %resets userpath to default value
userpath('clear')  %clears the userpath

The function restoredefaultpath restores the default path, which just includes the installed Matlab toolboxes and your userpath.  If you want to restore your path back to the original path, you can type the following:

restoredefaultpath; savepath

This function is handy in case you make manual changes to pathdef.m, or you just do not want to go to the trouble of removing each path you saved.

All of the functions mentioned in the first article and this article are useful and can be used at the command line to modify your search path.  There is another way to manage you search path, which is invoked by the  pathtool function or reached by clicking the File menu and then Set Path…Pathtool opens a dialog box which looks like this:

Here is a list of the buttons in the Set Path dialog box and what they do:

Add Folder… – Adds a folder to the top of the search path (similar to  addpath)

Add with Subfolders… – Adds a folder and its subfolders to the top of the search path (similar to addpath using genpath)

Move to Top – Moves the selected folder to the top of the search path

Move Up – Moves the selected folder up one level in the search path

Move Down – Moves the selected folder down one level in the search path

Move to Bottom – Moves the selected folder to the bottom of the search path

Remove – Removes the selected folder from the search path

Save – Saves the current search path (similar to savepath)

Close – Closes the Set Path dialog box

Revert – Reverts the search path to the state when it was last saved

Default – Sets the search path to the default path (similar to restoredefaultpath)

Help – Opens the help page on the Set Path dialog box

This concludes the series on managing your path in Matlab.  We have explained 7 functions, 2 environmental variables and 1 file that you can use to change your search path.  Modifying the search path makes software development faster and more efficient and is a skill that every Matlab programmer should have.

1 thought on “Managing Your Path in Matlab – Part II

Leave a Reply to Brad Brauer Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.