我使用mex函数将一些fortran代码转换为matlab,最后得到了myPET3copy.mexw64。我第一次调用这个函数,它运行良好,但随后我调用它第二次,它返回错误的结果为 NaN,第三次也是如此。另一个事实是,每次我重新编译这个 myPET3copy.mexw64 时,我第一次调用它时它又运行良好。有谁知道如何解决这个问题?任何帮助将不胜感激。
>> INPUT=[.42,.1,400.,.085,6000.,.675,1500.,.125,.125,300.,14.,.54,2.57,.33,.00036987];
>> OUTPUT=mexfunc(INPUT)
OUTPUT =
1.0e+06 *
Columns 1 through 7
0.0060 0.0000 0.0015 0.0498 0.0000 8.1509 0.0001
Column 8
0.0000
>> OUTPUT=mexfunc(INPUT)
OUTPUT =
Columns 1 through 6
6002 NaN 1500 NaN NaN NaN
Columns 7 through 8
NaN NaN
>>
以下是我的 mexFunction:
#include "fintrf.h"
subroutine mexFunction(nlhs,plhs,nrhs,prhs)
implicit none
mwPointer plhs(*),prhs(*)
integer*4 nlhs,nrhs
mwPointer mxGetPr
mwPointer mxCreateDoubleMatrix
!integer*4 mxIsNumeric
mwPointer mxGetM,mxGetN
mwPointer input_ptr,output_ptr
mwPointer mrows,ncols
mwSize size
COMMON/PAR/INPUT(15),OUTPUT(8)
real*8 INPUT,OUTPUT
mwSize :: ONE=1,EIGHT=8
integer :: ComplexFlag=0
mrows=mxGetM(prhs(1))
ncols=mxGetN(prhs(1))
size=mrows*ncols
input_ptr=mxGetPr(prhs(1))
call mxCopyPtrToReal8(input_ptr,INPUT,size)
plhs(1)=mxCreateDoubleMatrix(ONE,EIGHT,ComplexFlag)
output_ptr=mxGetPr(plhs(1))
call myPET3()
call mxCopyReal8ToPtr(output,output_ptr,EIGHT)
return
end