MATLAB: How to determine the MATLAB version when compiling C/C++ mex files

cdefineMATLABmexversion

MATLAB's C/C++ mex interface changes from time to time, with new releases of MATLAB. Functionality can be added, removed or changed. In order to write a C/C++ mex file which can be compiled by different versions of MATLAB, it might be necessary to have separate code for these different versions, selected using pre-processor macros. To do this, there needs to be a #define in mex.h which states the MATLAB version. Is there such a #define, and if so, what is it called?

Best Answer

This has been a problem for a long time now. And even if modern Matlab versions define the constant MX_API_VER, this is not defined in older versions.
I'm using a Matlab-function to call mex, which defines:
matlabV = [100, 1] * sscanf(version, '%d.%d', 2);
Args = {sprintf('-DMATLABVER=%d', matlabV)};
Then the constant MATLABVER is e.g. 802 for Matlab 8.02.