To choose the compiler in MATLAB, type:
in MATLAB Command Window and let MATLAB locate installed compilers. Then choose one of the listed compilers. If you want to use Visual Studio to build your MEX files, choose Visual C++ Compiler.
To set up a Visual Studio project for building and debugging “ordinary” MEX functions, see this article on The Code Project: Matlab 7.1+ and Visual Studio 2005. For Simulink S-functions, read on. The instructions have been tested with Visual Studio 9 (2008) and Matlab R2009a, but should work with any similar setup.
As project type, choose Win32 Console Application. In the project creation Wizard, under Application Settings select
LIBRARY DefFileName EXPORTS mexFunction
Now you should be able to build your project and get the .mexw64 (or .mexw32) file.
For details, see links in the Resources section.
When trying to run the S-function block in Simulink, you might receive error report of this type:
Error while obtaining sizes from MEX S-function <function name> in <function folder> MATLAB error message: Invalid MEX-file <MEX-file path> : the specified module could not be found
This error is, in most cases, caused by missing one or more dynamic-link libraires (-.dll) and can be fixed by installing Visual C++ runtime libraries.
If problem persists, there are two possible reasons:
Solution: Install Visual Studio 2008 Service Pack 1
Solution: You need to check dependencies of the MEX-file. You can use Dependency Walker for such purpose. Locate missing dlls and add path to the folder containing the dlls to DOS Path of Windows OS.
Note: Dependency Walker sometimes can't find the files (or dlls) that MATLAB can, such as libmex.dll or libmx.dll. Any error messages by Dependecy Walker about missing these files you can ignore, because MATLAB will find them (the same applies to dlls associated with Visual C++ runtime libraries, especially debug versions of dlls). Any other dependency error reported must be handled (hint: these errors often occur when accesing drivers of I/O devices on the computer).