MATLAB: How to change the MEX compilation directory for temporary object files created while building a MEX function

deffilelocationMATLABmexobjobjectobjs

I do not have the necessary permissions to access the default location used for temporary object (.obj) files when building MEX functions using the "mex" command. How do I change where the temporary object (.obj) files get placed?

Best Answer

The MEX command uses the Windows temporary folder as the location to store temporary object files when building MEX functions. 
In Windows, this location is defined by the "TEMP" environment variable. A typical value for the "TEMP" environment variable, in Windows Vista and above, is %USERPROFILE%\AppData\Local\Temp.
If you would like to change the value of the "TEMP" environment variable:
On Windows 10 and Windows 8:
1) In Search, search for and then select: System (Control Panel)
2) Click the *Advanced system settings* link
3) Click *Environment Variables...* and under the *User variables* sections click *New*
4) Enter *TEMP *for the variable name and a new folder path for the variable value
On Windows 7:
1) From the desktop, right click the *Computer *icon
2) Choose *Properties *from the context menu
3) Click the *Advanced system settings* link
4) Click *Environment Variables...* and under the *User variables* sections click *New*
5) Enter *TEMP *for the variable name and a new folder path for the variable value
You need to restart MATLAB for this change to take effect.