MATLAB: How to change the Temporary Directory that MATLAB uses

MATLAB

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?

Best 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:
MATLAB will check for the environment variable 'TMP' (getenv('TMP') )from the system and 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.
FOR WINDOWS: MATLAB similarly checks the 'TEMP' environment variables and if it is empty, MATLAB will use the present working directory(PWD).
In order to change the system setting, here is a quick example :
tempdir
clear all
setenv('TEMP','NEW_DIRECTORY_PATH') % TEMP for Windows
tempdir