MATLAB: Incorrect number of arguments into mex file

MATLABmexr2015br2016a

Hi All
I have a mex file that has worked for the last 10 years (up to R2015b) and now is giving me problems since I have upgraded my Matlab version. The problem is that I check the number of input arguments and when it is not equal to two it throws an error. When I call the mex function it now sometimes throws an error, where it does not seem to pass any arguments in. When I run through the debugger, stop at the mex function call, and then call, it seems to work. I am definitely passing in the correct arguments. The problem seems to be spurious and does not always show up. It seems to be some or other initialisation problem or something like this.
Any help would be appreciated
Etienne

Best Answer

Particularly for Fortran, you should always use the exact signature that is in the doc. Sometimes that signature changes from release to release, or maybe the default compiler options for mexing have changed. Either one of these can trip you up. E.g., look at this from your code:
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
!
!
!$ USE OMP_LIB
IMPLICIT NONE
MWPOINTER PLHS(*), PRHS(*), mxDuplicateArray
MWSIZE NLHS, NRHS
The signature from the doc is:
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
integer nlhs, nrhs
mwPointer plhs(*), prhs(*)
You've got NLHS and NRHS typed as MWSIZE, but the signature in the doc says the type should be integer. This could be a mismatch, since MWSIZE might be defined as an integer*8 but a default integer might be an integer*4. There is no automatic type promotion in Fortran like there is in C/C++, so this might cause a problem. It would be best to type NLHS and NRHS exactly like the doc states.