Fortran Mex File specified procedure could not be found error
Show older comments
I have a pretty basic Fortran mex file that contains a subroutine that I want to use to gateway into another subroutine that is defined in another file
#include "fintrf.h"
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
implicit none
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
mwPointer mxGetDoubles
mwPointer mxGetPr
mwPointer mxCreateDoubleMatrix
real*8 var1, var2, var3,
real*8 output(2)
call mxCopyPtrToReal4(mxGetDoubles(prhs(1)), var1, 1)
call mxCopyPtrToReal4(mxGetDoubles(prhs(2)), var2, 1)
call mxCopyPtrToReal4(mxGetDoubles(prhs(3)), var3, 1)
call other_subroutine(var1, var2, var3, output)
! What the subroutine arguments look like in the other file
!integer(4),intent(in) :: var1
!real(4),intent(in) :: var2, var3
!real(4),intent(out) :: output
plhs(1) = mxCreateDoubleMatrix(2,1,0);
call mxCopyReal4ToPtr(w, mxGetPr(plhs(1)), 2)
return
end subroutine mexFunction
and I am compiling with these arguments
-R2018a COMPFLAGS="/nologo /fpp /Qprec /MD /fp:source /assume:bscc $INCLUDE $COMPDEFINES"
as I am told it allows me use free form. However I am getting specified procedure could not be found when I try to run the compiled mex function. However when I comment out the call to the other subroutine I am able to run the function. The subroutine that I am trying to interface works as I have used it multiple times in a pure Fortran environment. I think my issue is similar to this where 3rd party code is attempted to be interfaced with MATLAB (in that link it is a C xml parsing lib while mine is something to compute wind speeds from data). I am not super familar with mex files as the syntax seems to be different than of "normal" Fortran where types are defined like real(8) :: var instead of what is shown above with real*8.
Also If I change the compile flags to just /fpp and $INCLUDE $COMPDEFINES I am actually able to run the mex function but it just produces garbage values.
2 Comments
James Tursa
on 21 May 2021
Edited: James Tursa
on 21 May 2021
How are you building the mex routine? What file is the other_subroutine( ) code in and how are you compiling/linking that in with your mex code? Does this other_subroutine( ) code use library code? If so, how are you linking that library code into your mex routine?
Also, you can't copy double precision variables to single precision variables the way you are attempting. Currently your code simply does a copy of the bits verbatim, so a double precision bit pattern when directly interpreted as single precision will be garbage. I.e., var1 is real(8) but the subroutine is expecting integer(4), so computations will be garbage. Similar for var2, var3, and output ... the types don't match. To fix this, you will either need to pass in int32 and single precision variables from MATLAB to your mex routine, or properly convert the double precision inputs to integer(4) and single precision before passing them on to your subroutine. If you need help with this part let me know.
Wei Yang Shen
on 21 May 2021
Answers (0)
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!