Clear Filters
Clear Filters

Why is my modified path not saved in MATLAB?

72 views (last 30 days)
I add a path using the "Set Path" GUI, and then save it. When I exit MATLAB and restart it, the added paths are no longer on the search path.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Feb 2010
The MATLAB path information is saved in the file "pathdef.m". When the "Save" button in the "Set Path" GUI is pressed, the location of the file pathdef.m is determined using the output of the function WHICH.
If there is a file called pathdef.m inside one of the directories that were just added to the top of the MATLAB search path, then this file is used to save the changes to the path instead of using the built-in file $MATLABROOT\toolbox\local\pathdef.m (where $MATLABROOT is your root MATLAB directory).
This can be verified by calling the following right after saving the path changes:
which pathdef -all
Normally, only one file should be found:
If multiple files are found, then the path changes are saved to the file that appears topmost.
Always avoid using file names that collide with built-in MATLAB files. The newly added directories could also be moved to the bottom of the search path, this way the function WHICH will find $MATLABROOT\toolbox\local\pathdef.m first, and will save the path changes there.

More Answers (1)

Jan on 6 Jun 2019
It is safer to work without admin privileges to avoid an accidental modification of Matlab's toolbox functions. A drawback is, that you cannot modify <matlabroot>\toolbox\local\pathdef.m file in without admin privileges. This file defines Matlab's path for all users on the local machine, so it is questionable if it should be used at all on a multi-user machine.
The current method to select the first pathdef.m file in the path at startup is working and can catch all needed situations, as long as the user has the privilege to modify the pathdef.m file in the installation folder and does this carefully. For users without admin privileges or with a less careful programming style (adding folders on top of Matlab's path - bad idea), the current method is not optimal. If Matlab is started in a specific folder, which contains a pathdef.m file also, there is a risk of confusion.
I solved the problem by opening Matlab once with admin privileges and add one folder at the bottom of the path:
addpath('D:\MFiles\Inits', '-end');
This is equivalent to editing the path in pathtool and pressing "Save".
This folder D:\MFiles\Inits\ contains files, which appends the user defined folders to Matlab's path. This allows to work e.g. with projects also and a kind of version control is possible also.
It would be smarter, if Matlab uses <matlabroot>\toolbox\local\pathdef.m at first and includes the userpath (see ) automatically, and then appends the folders defined in userpath/pathdef.m to the path.
Another option is to insert the user-defined changes of the path inside the startup.m function, which is called automatically from matlabrc.m on startup. Here the user has the full control without admin privileges also. So actaully there is no need to modify pathdef.m at all, but this happens in pathtool and so it became a standard.
See also these tools to manage the path:


Find more on Search Path in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!