Thursday, May 24, 2012

Matlab's userpath and how to use it

I've been having some trouble lately on my computer dealing with MATLAB's search path (specifically MATLAB R2011 A). I try to change the user path from the GUI via File -> Set Path..., which is the same as entering 'pathtool' in the Command Window, but my settings are never saved when I restart MATLAB. I suspect that the problem involves the permissions which are managed by Windows 7, which typically prevents me from changing files on my hard drive from within MATLAB.

I managed to change the user path by instead using the command sequence


where of course the second command points to my desired user path.

I discovered that the string containing of all the paths is contained in matlabroot/toolbox/local/pathdef.m, where matlabroot is the parent folder that contains the MATLAB program files. For example, my root folder is C:\Program Files\MATLAB\R2011a\. At the end of this file, there is the line

p = [userpath,p];

which appends the string in userpath to the front of the string of paths to all of the toolboxes (note that pathdef.m returns the string p).  Also of note is that pathdef.m is called by MATLAB's initialization m-file, matlabrc.m, which is located in the same folder and executed at MATLAB's startup.

Finally, there exists in the same directory an m-file called startupsav.m. It is a template which, when renamed to startup.m, contains any lines of code that one wishes to run at startup. I believe that these lines are executed once the Matlab GUI opens and well after matlabrc.m is executed.

I'm still not sure where userpath is defined.

Addendum: You may place your pathdef.m or startup.m files in the userpath directory, and I believe that these take precedence over those in the matlabroot/toolbox/local/pathdef.m file.

In my startup.m, I've added paths that contain files that I do not wish to be associated with MATLAB's toolboxes. The commands in this file look like

addpath some_directory\MatlabMPI\src\ -END

where the -END flag places this path at the end of the string returned by the path command.