0

I'm trying to set the temporary directory in MATLAB using setenv('TEMP','C:\Temp') but it seems to have no effect:

Code

t = tempdir
setenv('TEMP','C:\Temp');
t = tempdir

Output

t = C:\Users\KAR~1\AppData\Local\Temp\
t = C:\Users\KAR~1\AppData\Local\Temp\

I can't seem to find the reason why I am unable to set the Temp directory this way.

Thierry Dalon
  • 779
  • 5
  • 21
  • Inserting the line 'clear all' in the line above 'setenv('TEMP','C:\Temp');' seems to solve the problem. – Karl_Haberman Sep 05 '16 at 15:58
  • Please write your comment as an answer instead. – edwinksl Sep 05 '16 at 19:37
  • Hi, it is not a problem of the command sentenv but tempdir. (I would rename the title of your question.) Edit this function and have a look how it is coded: it uses a persistent variable. That's why your clear all seems to solve your issue. In your case setenv('TEMP','C:\Temp'); getenv('TEMP'); works properly. Note that the setenv function only change the variable for the MATLAB session / not in the Windows OS. – Thierry Dalon Sep 06 '16 at 07:18

2 Answers2

1

Changing the code to:

t = tempdir
clear all;
setenv('TEMP','C:\Temp');
t = tempdir

seems to solve the problem.

0

We can use 'clear tempdir' to clear the tempdir function alone (which, as an aside, is also used by tempname).

t = tempdir
clear tempdir;
setenv('TEMP','C:\Temp');
t = tempdir

'clear tempdir' specifically clears this function along with it's persistent variable. The presence of this persistent variable is causing matlab to remember the first value of the temporary folder environment variable that the tempdir function saw.

I would have posted this as a comment to Karl's answer, but I'm lacking in the reputation dept and still think it is valuable.