****The mex file with a different prhs****
Show older comments
Hi, I have a compiled mex file that gives rubbish results.
After some detective work, I realised that the computational subroutine worked fine and that the problem was with the prhs. Strangely, the prhs did not correspond to my input.
I think it has most probably got to do with the type (int32 or int64), although I'm can't be absolutely sure. Does anybody know why it happened?
>> mex Print.F
>> Print(1,3)
ans =
4613937818241073152
-------------------------------------------------------------------------
#include "fintrf.h"
C======================================================================
#if 0
C
C add.F
C .F file needs to be preprocessed to generate .for equivalent
C
#endif
C
C add.f
C
C Adds two integers
C
C======================================================================
C *Gateway routine*
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
C *Declarations*
implicit none
C *mexFunction arguments:*
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
C *Declare local variables for the mexfunction arguments*
mwPointer a, b
mwPointer c
mwSize mrows, ncols
mwSize size
mwSize :: one = 1
integer*4 classid
integer*4 :: mxREAL = 0
integer*4 mxIsInt64
C *Declare the symbolic names and types of this function*
integer mxIsNumeric
integer*4 mxClassIDFromClassName
mwPointer mxGetNumberOfElements
mwPointer mxCreateNumericMatrix
mwPointer mxGetData, mxGetPr, mxCreateDoubleMatrix
mwSize mxGetM, mxGetN
C *Verify MEX-File Input and Output Arguments*
if( nrhs /= 2 .or. nlhs > 1 ) then
call mexErrMsgTxt('Need 2 inputs and at most 1 output')
endif
if( mxGetNumberOfElements(prhs(1)) /= 1 .or.
+ mxGetNumberOfElements(prhs(2)) /= 1 ) then
call mexErrMsgTxt('Inputs must be scalar')
endif
C *Prepare in/out matrix:*
classid = mxClassIDFromClassName("int64")
plhs(1) = mxCreateNumericMatrix(one,one,classid,mxREAL)
a = mxGetPr(prhs(1))
b = mxGetPr(prhs(2))
c = mxGetPr(plhs(1))
C *Get the size of the input array.*
c mrows = mxGetM(prhs(1))
c ncols = mxGetN(prhs(1))
c size = mrows*ncols
C *Call the computational subroutine.*
call add(%val(a), %val(b), %val(c))
return
end
C-----------------------------------------------------------------------
C *Computational subroutine*
subroutine add(a, b, c)
integer*8 a, b, c
c = b
return
end
Accepted Answer
More Answers (0)
Categories
Find more on Fortran Source MEX Files 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!