This submission provides the following files to assist the programmer in creating robust C/C++ mex code that can compile and run under multiple MATLAB versions:
(1) matlab_version.h provides pre-processor code that defines the macro MATLAB_VERSION, which will contain the hex number equivalent of the MATLAB version that is being used for the compile (e.g., a value of 0x2015b would indicate MATLAB version R2015b). It also defines the macro TARGET_API_VERSION to differentiate between the R2017b and R2018a API libraries being used. And it contains a prototype for the matlab_version() function that is contained in the matlab_version.c file.
(2) matlab_version.c provides code for the matlab_version() function, which returns the hex number equivalent of the MATLAB version that is currently being run. (via a mexCallMATLAB callback)
(3) matlab_version_test.c provides code for a mex routine that tests matlab_version.h and matlab_version.c
(4) matlab_version_test.m provides m-code that will automatically compile the matlab_version_test.c file.
To test all of this, simply type at the command line:
There are also extensive comments at the front end of the matlab_version.h file that describe an assortment of mxArray and API changes over the years. The idea is that the macro MATLAB_VERSION can be used to determine whether various library functions or features that the programmer is depending on are actually available in the version being used for the compile. E.g., the programmer could use an #if - #else - #endif block to conditionally compile different code depending on the value of MATLAB_VERSION. And the matlab_version( ) function result can be used to determine if a MATLAB function the programmer wants to call with mexCallMATLAB is actually available in the current version of MATLAB being run.
Tested under various Win32 and Win64 versions of MATLAB from R2009a - R2018a.
James Tursa (2022). C Mex MATLAB Version (https://www.mathworks.com/matlabcentral/fileexchange/67016-c-mex-matlab-version), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!