MATLAB: Is MinGW compiler not recognized anymore after restarting MATLAB

MATLAB

After installing the "MATLAB Support for MinGW-w64 C/C++ Compiler" support package in MATLAB, the command "mex -setup" reports that the compiler is correctly installed.:
>> mex -setup
MEX configured to use 'MinGW64 Compiler (C)' for C language compilation.
However, after I restart MATLAB, the same command does not detect the MinGW compiler as installed anymore:
>> mex -setup
No supported compiler or SDK was found. You can install the freely available MinGW-w64 C/C++ compiler; see
Install MinGW-w64 Compiler. For more options, see 
http://www.mathworks.com/support/compilers/R2015b/win64.html.
But when I check the Add-On Explorer, or run the command:
>> matlabshared.supportpkg.getInstalled
the MinGW Support Package is still shown as installed. Why does MATLAB not detect the MinGW compiler anymore after a restart of MATLAB?

Best Answer

When installing the MinGW compiler, the environment variable MW_MINGW64_LOC is set with the path to the MinGW installation folder. On some systems where users have limited privileges, there may be policies in place preventing environment variables from being set. If this environment variable is not staying set following the installation, or after restarting MATLAB, the behavior described above may occur.
Running the 'mex -setup' command in verbose mode
>> mex -setup -v
will display whether the MW_MINGW64_LOC environment variable is set. If this is not the case, the output will look as shown below:
... Looking for compiler 'MinGW64 Compiler (C)' ...
... Looking for environment variable 'MW_MINGW64_LOC' ...No.
The solution is to have an administrator set the environment variable on your system. Your local IT administrator should be able to help with this. On Windows 10, you can find this panel by entering "Edit the system environment variables" in the Windows search:
The environment variable should have the following name and value:
  • Variable Name: MW_MINGW64_LOC
  • Variable Value: <SupportPackageRoot>\3P.instrset\mingw_w64.instrset
where <SupportPackage Root> is the output received for the command:
>> matlabshared.supportpkg.getSupportPackageRoot
Related Question