MATLAB only supports specific versions of MinGW. R2017b and later supports MinGW 5.3, while R2017a and prior support MinGW 4.9.2. Other versions of MinGW downloaded from third party sources are not qualified to work with MATLAB. Installation instructions for the various release follow:
R2015b and R2016a
Download the MinGW installer from the link below:
This installer requires a MathWorks account and involves registration/configuration after the installation, which will allow MATLAB recognize MinGW.
R2016b and later
Search for "MATLAB Support for the MinGW-w64 C/C++ Compiler from TDM-GCC" in the MATLAB Add-ons menu. You can then install the support package from the Add-on explorer.
More information on accessing the Add-ons menu can be found in the following link: