You should use "implicit none" in your code ... it is one of the few friends you have in Fortran.
You should declare all of your functions to make sure the compiler knows the type that is returned.
You should never use constants for arguments to the API functions ... always use variables to ensure that the argument types match.
E.g.,
The documentation shows the following signature for mexFunction:
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
integer*4 nlhs, nrhs
mwPointer plhs(*), prhs(*)
So use integer*4 for the nlhs and nrhs, not integer. Probably not a source of the error since I would expect integer to be integer*4 even on a 64-bit system, but why not use the published signature?
Then consider mxGetM and mxGetN. Since you don't declare them they will get an integer return type by default (probably integer*4 on your system), but it is entirely possible that the actual return type is integer*8 on your 64-bit system. So declare them properly to avoid this potential system crashing mistake:
mwPointer, external :: mxGetM, mxGetN
Then consider the mxCopyPtrToReal8 calls you make using a constant in the last argument. That constant will likely be an integer*4, but the signature is:
subroutine mxCopyPtrToReal8(px, y, n)
mwPointer px
real*8 y(n)
mwSize n
That mwSize might be an integer*8 on a 64-bit system. So this call:
call mxCopyPtrToReal8(np,nr,1)
should look like this instead:
mwSize numel
:
numel = 1
call mxCopyPtrToReal8(np,nr,numel)
I can't comment on anything else you have posted until you format it correctly with the "{ } Code" button. It is unreadable until you do this.
Best Answer