****The mex file with a different prhs****
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Titus Edelhofer
2014-6-4
Hi,
it's not the data into the mex function (they are of type double, that's fine), but it's the plhs you have wrong: you create a numeric matrix of type int64, therefore you can't use mxGetPr but need to use mxGetData. And if you want to call your function with int64 you need to do explicitly, i.e.
Print(int64(1), int64(3))
Titus
6 个评论
James Tursa
2014-6-5
Go ahead and look through the MATLAB provided examples, but keep in mind they are not all written properly. You might also look at this FEX submission for a Fortran 95 interface (although I don't think it has been entirely updated for 64-bit systems yet):
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!