MATLAB: How to find Matlab version to be used in the mex file header

matlab versionmex

I create a mex file that compiles and works properly in R2010b and up, versions I have at my work. But I need the same file be used in R2010a, since that's the version I have in my laptop. If I used in R2010 I have an error. I found the code need it to make it work. But every time I switch I have to comment and uncomment. Is there any way to find Matlab's version in the header? Let me give some code:
#include <stdlib.h>
#include <vector>
#include <queue>
#include <limits>
#include <utility>
#include <map>
#include <assert.h>
#include <stdio.h>
/* #ifdef _CHAR16T //for matlab 2010a
#define CHAR16_T
#endif */
#include "mex.h"
#include "matrix.h"
using namespace std;
........code ............
The commented section is my fix. I want to find Matlab's version and put an if statement so it automatically applies my fix. I try this:
int i;
int j;
sscanf(version,'%d.%d %*s',&i, &j)
#if i<=7 && j<11
#ifdef _CHAR16T //for matlab 2010a
#define CHAR16_T
#endif
#endif
but it did not work since c++ did not find the "version" variable defined in matlab. Does somebody know how to do that? I really appreciate any help!

Best Answer

Does the problem concern the Matlab version, or is it caused by different compilers?
Would it work to check if CHAR16_T is defined already?
#ifndef CHAR16_T
Butr if the problem really concerns the Matlab version, I suggest to append a flag to the MEX call:
function myMex(varargin)
V = [100, 1] * sscanf(version, '%d', 2); % Consider 7.10!
if V < 711
Flag = {'-DNEED_CHAR_16_T'};
else
Flag = {};
end
mex(varargin{:}, Flag{:});
I started with some equivalen flags to distinguish Matlab 5.3 and 6.5. Meanwhile my enhanced MEX function has 500 lines, considers different mexopt.bat files, e.g. enable SSE2 commands under 32-bit Matlab, but avoid this under 64 bit, if MSVC2010 ist used, etc.