MATLAB Answers

How do I change the MATLAB preferences directory location?

134 views (last 30 days)
I would like to know how I can change the location of the preferences directory that MATLAB uses by default to a custom location.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Jul 2021
Edited: MathWorks Support Team on 2 Jul 2021
The following is not an officially supported workflow but there may be some situations in which this can used as a workaround, for example to:
• Correctly save command history when running 32-bit and 64-bit copies of the same MATLAB release
• Work around specific issues associated with the default location of the MATLAB preferences folder, such as non-writable directories on a web server or extremely long folder names.
The MATLAB preferences folder should only be changed if absolutely necessary, because changing it introduces the following limitations:
• Without additional action (detailed below), multiple releases of MATLAB installed on one computer will share a single preferences folder. This can cause unpredictable results, and is not recommended.
• Preferences stored in a custom folder location are not migrated when installing a subsequent release of MATLAB.
You can determine the location of the MATLAB preferences folder by running the PREFDIR command at the MATLAB prompt.
To override the default location, create an environment variable named MATLAB_PREFDIR. MATLAB reads this value at startup. Since multiple installations of MATLAB may not share a single preferences folder, set this environment variable when starting a particular instance of MATLAB. The procedure to set an environment variable at start-up varies between Windows, Mac, and Linux. Each OS is discussed separately below.
On
Windows
:
On Windows, start MATLAB from a batch file that sets the MATLAB_PREFDIR environment variable. To create this file, determine the location of the MATLAB executable for the specific installation of MATLAB by typing the command:
fullfile (matlabroot, 'bin', 'matlab')
To create the batch file, save the following commands in a text file. You can give the file any name, ending with the extension “.bat”. Replace <New preference folder location> with the full path for the folder you would like to use for your preferences. Replace <MATLAB executable location> with the location determined using the above command (keep the quotation marks). To launch MATLAB, double-click on the file you created.
set MATLAB_PREFDIR=<New preference folder location>
"<MATLAB executable location>"
Example:
set MATLAB_PREFDIR=C:\MATLABPreferences
"C:\Program Files\MATLAB\R2010b\bin\matlab.exe"
Note that there are no spaces around the character '='. Placing spaces around the equals (e.g., set MATLAB_PREFDIR = C:\MATLABPreferences) will cause Windows to ignore the SET command.
On Mac:
On Mac, modify the MATLAB configuration information. You must be an administrator on your Mac to perform this task.
In the MATLAB Command Window:
cd(fullfile(matlabroot, 'Contents'))
edit Info.plist
This opens an XML file in the MATLAB editor. Make a backup copy of the file, and be careful when editing this file, as errors can prevent OS X from launching MATLAB. Find the “LSEnvironment” section. It will look similar to the following:
<key>LSEnvironment</key>
<dict>
<key>MATLAB_USE_USERWORK</key>
<string>1</string>
</dict>
Insert the following two lines on the line immediately following <dict>, as shown. Replace New preference folder location with the folder you would like to use for your preferences.
<key>LSEnvironment</key>
<dict>
<key>MATLAB_PREFDIR</key>
<string>New preference folder location</string>
<key>MATLAB_USE_USERWORK</key>
<string>1</string>
</dict>
Save the file. Then, in the MATLAB Command window, type:
system(['touch ' matlabroot])
Exit MATLAB and restart for the changes to go into effect.
On Linux:
On Linux, MATLAB will be started from a command shell after setting the MATLAB_PREFDIR environment variable. You will first need the location of the MATLAB executable for the specific installation of MATLAB. The following MATLAB command will display this location:
fullfile (matlabroot, 'bin', 'matlab')
The exact syntax to use depends on the shell that you are using. Run the following commands from within your command shell. Replace <New preference folder location> with the full path for the folder you would like to use for your preferences. Replace <MATLAB executable location> with the location determined using the above MATLAB command.
sh / bash:
$ export MATLAB_PREFDIR=<New preference folder location>
$ <MATLAB executable location>
csh / tcsh:
$ setenv MATLAB_PREFDIR <New preference folder location>
$ <MATLAB executable location>
  1 Comment
Peter Dowell
Peter Dowell on 5 Aug 2019
Note that if your path contains spaces you need to call set with quotation marks. If you don't the environment variable won't be setup correctly.
For example,
set "MATLAB_PREFDIR=C:\foo with spaces\bar\"
"<MATLAB executable location>"

Sign in to comment.

More Answers (2)

Adam Pazdzior
Adam Pazdzior on 22 Nov 2016
Is it possible to somehow apply this change to the main .exe file?
  1 Comment
Jonathan Kwang
Jonathan Kwang on 7 Dec 2016
This does not look to be possible as there is no MATLAB startup flag you can use to set the preference directory with the main .exe file

Sign in to comment.


Feng Liu
Feng Liu on 9 Jul 2018
My answer is to delete the .matlab folder in your home folder. Your prevous openned folder won't be memorized and Matlab will automatically create a new one

Community Treasure Hunt

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

Start Hunting!