How do I change the Temporary Directory that MATLAB uses?

By default, MATLAB writes temporary files to /tmp/ on a LINUX system and the C:\Temp folder on a Windows System.
How can the MATLAB temporary directory be set to a different value? Can this be done at the system level, or by individual users in their MATLAB sessions?

 Accepted Answer

This is a setting that that MATLAB finds from the system environment variables. You can set or query the environment variable using the SETENV and the GETENV functions from within MATLAB as well. Please see below for the system level settings.

FOR LINUX/MAC:

MATLAB will check for the environment variable 'TMPDIR' (getenv('TMP')) from the system and if this is empty MATLAB will check environment variable 'TMP' if this is empty, MATLAB will use '/tmp/' as the temporary directory. You can set the 'TMP' environment variable to some other directory and MATLAB will use that as the TEMPDIR after a restart.

FOR WINDOWS:

MATLAB similarly checks the 'TMP' environment variable first and if it is empty, MATLAB will check the 'TEMP' environment variable. If this is empty, MATLAB checks the environment variable 'USERPROFILE'. If this is also empty, MATLAB will use 'C:\temp' as tempdir. MATLAB will use that as the TEMPDIR after a restart.
In order to change the system setting of a running MATLAB session which does not need a restart, here is a quick example:
The 'clear' function call is necessary to reset the tempdir state in order to refresh the tempdir path when changing the environment variable in a running session.
tempdir %show unchanged tempdir
clear tempdir
if ispc %check if Windows
    setenv('TMP','NEW_DIRECTORY_PATH')
else
    setenv('TMPDIR','NEW_DIRECTORY_PATH')
end
tempdir %show and refresh tempdir with new path

5 Comments

For Linux and OS-X, the default directory the code actually uses is /tmp not the /TMP that is described above.
Starting in 2017, the MATLAB starter script now uses the following algorithm for determining the temporary directory:
  1. if the TMPDIR environment variable exists, use it
  2. else if the TMP environment variable exists, use it
  3. else use /tmp
Notice in the answer,
"You can set the 'TMP' environment variable to some other directory and MATLAB will use that as the TEMPDIR after a restart."
So you need to set TEMPDIR or TMP outside of MATLAB, so that it exists at the time MATLAB starts.
If you want to be able to have that apply to MATLAB started from icon (including the dock) then you will need to set a permanent environment variable; see the first answer in https://apple.stackexchange.com/questions/289060/setting-variables-in-environment-plist for how to tell launchctl to update the permanent environment variables.
On my Mac, /private/tmp shows up as rwxrwxrwt but I suspect it is really rwxrwxrwxt . The numeric form of that permission would be 1777 . The 't' permission prevents other (non-priviledged) users from removing files they do not own.
IMHO: The tempdir function should not cache the environment variable at all, so it always retrieves the correct value without having to clear persistent variables.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2009a

Community Treasure Hunt

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

Start Hunting!