How to get Matlab Version at Mex Compilation Time?

7 views (last 30 days)
Hi everyone,
I've read a great number of topics on the subject but nothing seems to solve my problem. I'm adding new datatypes to a software using Matlab and Simulink and in one of my C - SFunctions i have to use ssRegisterDataTypeInteger() which is a new function added in Matlab2020a. So i have to make sure that we're using Matlab2020a or earlier with a preprocessor instructions in my C file.
I can't modify the building process to check matlab version with matlab functions, i've also looked at James Tursa codes but it's only working for matlab older than 2018b. If you can give me any lead on a way to solve my problem, i'll be thankful.
Greetings,
B.Daniel

Accepted Answer

James Tursa
James Tursa on 18 Nov 2020
Edited: James Tursa on 18 Nov 2020
My MATLAB version code works for me in R2020a:
>> mex matlab_version_test.c
Building with 'Microsoft Visual C++ 2015 (C)'.
MEX completed successfully.
>> matlab_version_test
Compiled under MATLAB_VERSION = R2020a
TARGET_API_VERSION value = 700
TARGET_API_VERSION option = -R2017b
Running under matlab_version = R2020a
>> mex matlab_version_test.c -R2018a
Building with 'Microsoft Visual C++ 2015 (C)'.
MEX completed successfully.
>> matlab_version_test
Compiled under MATLAB_VERSION = R2020a
TARGET_API_VERSION value = 800
TARGET_API_VERSION option = -R2018a
Running under matlab_version = R2020a
>> version
ans =
'9.8.0.1323502 (R2020a)'
The code is:
#include "mex.h"
#include "matlab_version.h"
#include "matlab_version.c"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char *Roption[] = {"-R2017b","-R2018a"};
mexPrintf("Compiled under MATLAB_VERSION = R%x\n",MATLAB_VERSION);
mexPrintf("TARGET_API_VERSION value = %4d\n",TARGET_API_VERSION);
mexPrintf("TARGET_API_VERSION option = %s\n",Roption[TARGET_API_VERSION==R2018a]);
mexPrintf("Running under matlab_version = R%x\n",matlab_version());
}
How exactly are you using my header files? I am curious as to why they are not working for you since they seem to work fine in my simple mex tests.
The include files can be found here:
  1 Comment
Brieuc DANIEL
Brieuc DANIEL on 18 Nov 2020
I've only read your code i haven't tried it yet, and i haven't any way to find how you handle Matlab2020a.
Thanks a lot i'm gonna try this as soon as possible !

Sign in to comment.

More Answers (1)

Andrew Flewellen-Gore
Andrew Flewellen-Gore on 18 Nov 2020
I would try out James's solution above if you are able. It looks like he checks for historical definition "markers" in order to infer the release version of MATLAB at the pre-processor stage. Quite cool! I can see how this is very useful and will create an enhancement ticket to see if MathWorks can provide an official way to check the MATLAB version at c mex preprocessing time.
Anyhow, if james's solution does not work for you or if you can't use his code for some reason, I would suggest implementing a workaround completely in c code, though these solutions will be pre-processor specific, compiler specfic, and or platform specific.
1) You could try weakly defining the function and only run the function if it is over-ridden by a strong definition. I tried this out and it worked well for me on linux with gcc when translated to a sample c mex file.
-----------example---c-code-------------------
void myfunc() __attribute__((weak)); // weak declaration must always be present
// optional strong definition:
void myfunc() { ... }
int main() {
if (myfunc){
// If execution reaches here, myfunc has been strongly declared or defined
myfunc(); // run the function
}
else{
// myfunc was not strongly declared or defined. Put fallback logic here or error.
}
}
--------------------------------------------------------
2) Another solution would be to dynamically load the function you want by name, and to only run the function if you find it successfully. For example on linux or mac you should be able to use dlopen() to access the symbols you want and dlsym() to get the address of the particular function symbol you want to use. This should compile and this way if you try to access a symbol that does not exist you can error out gracefully.
scroll down to "EXAMPLES" and the first example is pretty good. If you need this to work on windows you would need to use LoadLibrary() and GetProcAddress() I believe.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!