MATLAB: Are changes lost in MATLAB 7.0 (R14) when using the “!” (bang) operator or the SYSTEM function to change environmental variables

bangdosenvenvironmentexclamationMATLABsystem

I am using the ! (bang) operator to change system environmental variables. The command executes without problem, but the variables are not changed afterwards.

Best Answer

This information has been added to the documentation in Release 14 Service Pack 2 (R14SP2). For previous releases, please read below for any possible workarounds:
When you use the ! (bang) operator or SYSTEM, a new thread is created for the commands. So, in the new process, the environmental variables are changed. However, once the commands are completed, the process is ended. When you check afterwards, the environmental variables are unchanged because you are looking at the same thread as before.
In order to execute commands with specific environmental variables set, place all of the commands inside the call to SYSTEM. One helpful note, you can separate commands in DOS using an ampersand (&) and a semi-colon (;) on UNIX.
Alternatively, you can set the environmental variables before starting MATLAB.