Why is my modified path not saved in MATLAB?

57 views (last 30 days)
I am modifying my MATLAB path by using either the "Set Path" button in the toolbar, or by using the "addpath" and "savepath" commands in the MATLAB Command Window. However, when I exit MATLAB and restart it, the MATLAB search path does not reflect the changes that I had made.
Why are my MATLAB path modifications not saved in the new session of MATLAB?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 1 Oct 2024
Edited: MathWorks Support Team on 1 Oct 2024
The MATLAB path information is saved in the file "pathdef.m". When the "Save" button in the "Set Path" tool is pressed, or when using the "savepath" command, the location of the file "pathdef.m" is determined using the output of the function "which".
If there is more than one file called "pathdef.m" on your MATLAB search path, then the file at the top of the search path 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).
In other words, your changes to the MATLAB path are saved in the
topmost file
 listed when you run the following command in the MATLAB Command Window:
which pathdef -all
However, upon startup, MATLAB builds the search path from the "pathdef.m" file located in the $MATLABROOT\toolbox\local directory. In order for your path changes to be saved for when you reopen MATLAB, you need to ensure that your changes are saved to this "pathdef.m" file.
Therefore, to resolve the issue of MATLAB not saving the path changes, try one of the following:
  1. Rename all files called "pathdef.m", except the "pathdef.m" file stored in the MATLAB root directory. In general, it is good practice to avoid using filenames that collide with built-in MATLAB files.
  2. If you are using the "Set Path" interface from the toolbar, save your changes to the MATLAB path, and then copy the topmost "pathdef.m" file to the $MATLABROOT\toolbox\local directory. This will ensure that MATLAB will find your path changes upon startup.
  3. If you are using the "savepath" function, include the "outputFile" argument to specify that you want to save the current path to $MATLABROOT\toolbox\local\pathdef.m. To do so, you can use the following syntax:
       
savepath $MATLABROOT\toolbox\local\pathdef.m
For more information on the "savepath" function, please refer to the following documentation page:
https://www.mathworks.com/help/matlab/ref/savepath.html
Note that you can find your MATLAB root directory by using the command "matlabroot" in the MATLAB Command Window.

More Answers (1)

Jan
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');
savepath;
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 https://www.mathworks.com/help/matlab/ref/userpath.html ) 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:

Categories

Find more on Search Path in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!