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 0C C add.FC .F file needs to be preprocessed to generate .for equivalentC #endifC C add.fCC Adds two integersC C======================================================================C *Gateway routine* subroutine mexFunction(nlhs, plhs, nrhs, prhs)C *Declarations* implicit noneC *mexFunction arguments:* mwPointer plhs(*), prhs(*) integer nlhs, nrhsC *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 mxIsInt64C *Declare the symbolic names and types of this function* integer mxIsNumeric integer*4 mxClassIDFromClassName mwPointer mxGetNumberOfElements mwPointer mxCreateNumericMatrix mwPointer mxGetData, mxGetPr, mxCreateDoubleMatrix mwSize mxGetM, mxGetNC *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') endifC *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*ncolsC *Call the computational subroutine.* call add(%val(a), %val(b), %val(c))
return endC-----------------------------------------------------------------------C *Computational subroutine* subroutine add(a, b, c) integer*8 a, b, c c = b return end
Best Answer