Why can't I save my MATLAB path on Windows with User Account Control (UAC) enabled?

228 views (last 30 days)
I am trying to save the MATLAB path on Windows with the User Account Control (UAC) set to "on".
MATLAB is installed in a directory protected by UAC (e.g., C:\Program Files).
If I try to execute the function 'savepath', my path is not saved and I receive the following warning:
>> savepath
Warning: Unable to save path to file 'C:\Program Files\MATLAB\R2021b\toolbox\local\pathdef.m'.
You can save your path to a different location by calling SAVEPATH with an input argument that
specifies the full path. For MATLAB to use that path in future sessions, save the path to
''pathdef.m'' in your MATLAB startup folder.
If I try to save the MATLAB path from the Set Path dialog, I get the message:
MATLAB cannot save changes to the path.
The path file, pathdef.m, might be read-only or might be in a directory for which you do not
have write access.
You can save pathdef.m to a different location. In order for MATLAB to use that pathdef.m
in future sessions, you need to save it in your MATLAB startup directory (the directory
from which you start MATLAB).
Would you like to save pathdef.m to another location?
I have full administrative privileges on this computer.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Jun 2023
Edited: MathWorks Support Team on 5 Jun 2023
By default, the "pathdef.m" file containing the MATLAB user path is located in the MATLAB installation folder. If MATLAB is installed in the default directory (C:\Program Files\...) with User Account Control (UAC) enabled, write access to "pathdef.m" will be blocked.
 

Temporary Solution for Current MATLAB Session

Note that it is not mandatory to save the MATLAB path for it to take effect in your current MATLAB session. You can make temporary changes to the MATLAB path which will persist for your current session. All changes will take effect immediately.
Apply temporary changes by adding a folder with the Set Path dialog and then "Close" the window without clicking "Save":
The same can be accomplished programmatically by using the 'addpath' function without calling 'savpath' at the end. See: https://www.mathworks.com/help/matlab/ref/addpath.html

Permanent Solution for Future MATLAB Sessions

If you wish to have a permanent solution that automatically sets your custom MATLAB path for future MATLAB sessions, consider one of the following workarounds:
  • Option 1: Add folders automatically at MATLAB startup
  • Option 2: Add folders automatically at project startup
  • Option 3: Save pathdef.m in your MATLAB startup folder

Option 1: Add folders automatically at MATLAB startup

MATLAB offers several approaches to automatically execute commands at startup. One easy way to achieve this is to create a startup.m file containing 'addpath' commands. See:https://www.mathworks.com/help/matlab/ref/startup.html
Other more advanced ways would be to use the MATLABPATH environment variable, or to launch MATLAB from the Windows or Desktop shortcut with additional "addpath" commands.
Refer to the following documentation for an overview of how to specify MATLAB startup options:
 

Option 2: Add folders automatically at project startup

A variation of Option 1 would be to add folders to the MATLAB path on a project basis using MATLAB Projects and Simulink Projects. Learn more about project paths here:
 

Option 3: Save pathdef.m in your MATLAB startup folder

If you wish to be able to permanently save the MATLAB path, you need to store pathdef.m in a folder that is not protected by the UAC and where you have full read/write access. In order for MATLAB to use this pathdef.m in future sessions, you need to save pathdef.m in your MATLAB startup folder.
The location of the MATLAB startup folder depends on how you start MATLAB. If you launch MATLAB by double-clicking on a Windows desktop icon shortcut, you can specify the MATLAB startup directory in the HOME Tab under Preferences > MATLAB > General > Initial Working Folder.
A recommended location for the MATLAB startup directory is the Windows user directory:C:\Users\jondoe\Documents\MATLAB
Refer to the following documentation to learn more about the MATLAB startup folder:
MATLAB Startup Folder 
Steps to change the location of pathdef.m:
  2 Comments
Abhishek Pandey
Abhishek Pandey on 6 Jun 2016
Hi,
This probably means that you do not have administrator rights in your system. You basically need permissions to edit the pathdef.m file which is in your Windows directory.
Hope this helps!
- Abhishek

Sign in to comment.

More Answers (1)

yunhui zhou
yunhui zhou on 19 Mar 2020
Edited: yunhui zhou on 19 Mar 2020
Please note the following words in error message: " You can save pathdef.m to a different location, In order for MATLAB to user the pathdef.m in future sessions, you need to save it in your MATLAB startup directory (the directory from which you start MATLAB). Would you like to save pathdef.m to another location?".
Just copy and save pathdef.m to your user directory (for example "C:\Users\<Your_Name>\Documents\MATLAB") and you can save your path without messing the MATLAB path for other users of your computer.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!