How do I change MATLAB's default path?
MATLAB: How to change MATLAB’s default path
MATLAB
Related Solutions
The ability to select an arbitrary location for the MATLAB path file 'pathdef.m' is not available in MATLAB 8.1 (R2013a). One possible workaround is detailed below.
By default, the 'pathdef.m' file may be located in either the '$MATLABROOT/toolbox/local' directory or the '$USERPATH' directory, where $MATLABROOT and $USERPATH are the directories displayed after entering the commands
matlabroot
and
userpath
at the MATLAB command line respectively.
The 'pathdef.m' file can be manually copied from either of these directories and placed in an arbitrary directory. To use the copied 'pathdef.m' file at initialization, create a MATLAB file titled 'startup.m' and place it in the '$USERPATH' directory. Copy the lines of code below to the file. If a 'startup.m' file already exists in '$USERPATH', add the lines of code to the end of the file:
% save starting directory
startdir = pwd ;% specify arbitrary directory containing pathdef.m
pathdir = '<MODIFY>' ;% change directory so that target 'pathdef.m' has highest precedence
% in the MATLAB search path
cd(pathdir)% set the MATLAB search path using PATH
path(pathdef)% change directory back to the starting directory
cd(startdir)
Make sure to modify the string marked "MODIFY" in the above code to the directory to which the 'pathdef.m' file was copied. The 'startup.m' script will be automatically executed upon MATLAB initialization.
The MATLAB search path is the list of directories in which MATLAB expects to find MATLAB files. When searching for a MATLAB file, MATLAB will search these directories in order, using the first one it finds.
For a complete discussion on the MATLAB search path, refer to the MATLAB documentation:
>> doc path
The current directory is always searched first, before any directories in the search path.
The MATLAB search path is set on startup. Under Unix, it is set by the "matlab" Bourne shell script which invokes the binary for your specific architecture. The MATLAB search path also always includes a directory called "matlab" under your home directory (if it exists). Under Windows, it is set in the MATLABRC.M file, which is invoked on startup. On the Macintosh, it includes all folders under the MATLAB folder.
You can add your own directories to the search path using the 'path' command. See "help path" for a review of the syntax. There are two notes about adding directories which are not necessarily obvious from the online help.
1) To add a directory to the search path automatically on startup, you can put the path command in a file called "startup.m". This file must always be available when MATLAB starts: it must either be in the directory in which MATLAB is started or in a directory already in the MATLAB path. Under Unix and Mac OS X, it is commonly placed in the matlab directory under your home directory. Under Windows, it is commonly placed either in C:\MATLAB\TOOLBOX\LOCAL, or in the directory which MATLAB starts in. This latter can be set by looking at the "properties" of the MATLAB startup icon shortcut or Start Menu item (Windows 95/NT right click the shortcut or Start Menu item).
As an example, the following might appear in your startup.m file:
path(path, '/home/john/mfiles'); % Unix, or
path(path, 'c:\matlab\mfiles'); % Windows
2) Macintosh users can manipulate the MATLAB search path using the File --> Set Path... menu item.
3) Unix users can access many important directories using the 'getenv' function. This will return environment variables, which will contain important directories. For example, to add your home directory to the MATLAB search path, you can use the following:
path(path, getenv('HOME'))
This can be useful in cases where automounting makes directory names look different depending on where you have logged in.
At the present time, the tilde ~ (commonly used as an abbreviation for the Unix home directory) does not work in setting the MATLAB search path.
Best Answer