MATLAB Answers

How do I change the Temporary Directory that MATLAB uses?

219 views (last 30 days)
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

MathWorks Support Team
MathWorks Support Team on 4 Apr 2017
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.
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 :
clear all
setenv('TEMP','NEW_DIRECTORY_PATH') % TEMP for Windows
Ricardo Mejia-Alvarez
Ricardo Mejia-Alvarez on 2 May 2021
this didn't work for me.
I'm using Matlab in OSX
tempdir is the same after running the setenv function.
I have used 'TEMP' and 'TMP' as the name, and the full path to a folder that I created
ans = '/private/var/folders/ch/3b7fs15d07v7p1jg2ml899x00000gn/T/'
clear all
setenv('TMP','/Volumes/Promise_Pegasus/MatlabTemp') % TEMP for Windows
ans = '/private/var/folders/ch/3b7fs15d07v7p1jg2ml899x00000gn/T/'

Sign in to comment.

More Answers (4)

Alexander Ernst
Alexander Ernst on 16 Jan 2020
Edited: Alexander Ernst on 16 Jan 2020
Dear Mathworks Support Team,
I have been using the above posted code to set 'TEMP' to a new directory in Matlab R2017a (Windows 10).
Now in Matlab 2019a it is not working anymore. Could you help me with another solution?
clear all
setenv('TEMP','NEW_DIRECTORY_PATH') % TEMP for Windows

p fontaine
p fontaine on 5 Jan 2021
instead of

Martin Kotte
Martin Kotte on 4 Mar 2021
Hi all,
I'm using 2017a and have following problem:
clear all
setenv('TEMP','NEW_DIRECTORY_PATH') % TEMP for Windows
code works, temp folder is set correctly. But: there are no files stored in the new directory, especially large dmr files are still stored in the original temp folder (...appData\Temp). Any hints how to solve this?
  1 Comment
Thiviya Navaneethan
Thiviya Navaneethan on 16 Mar 2021
Hi Martin,
Please reach out to MathWorks Technical Support, they may be able to help you with this issue.

Sign in to comment.

Kevin on 27 May 2021
Edited: Kevin on 27 May 2021
If you are in a linux environment on a cluster be aware MATLAB seems to have an undocumented list of environment variables it checks to get the value of "tempdir". On a Linux cluster you need to set "TMPDIR" NOT "TMP" because the value of "TMPDIR" is checked first. In a job environment that may be set by the scheduler on job execution, and if it's set, regardless of the value you set "TMP" to, the value of "TMPDIR" is used.
Setting TMPDIR in Windows has no effect on the environment though, and TMP must be used for any change to happen.
This should be better documented on Mathworks' side.


No tags entered yet.




Community Treasure Hunt

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

Start Hunting!