我正在编写一个 MEX 文件,其中涉及从 Matlab 读取稀疏矩阵,但每次运行程序时,Matlab 都会崩溃。然后我写了一个测试文件,发现问题正好出在mxGetIr命令上。我不知道为什么会这样。请帮助我...非常感谢!
我在 Mac OS X 10.8.1 的 Matlab 2013a 下工作。
#include "fintrh.h"
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
mwpointer plhs(*), prhs(*)
integer nlhs, nrhs
integer temp, nzmax
integer, dimension(2) :: dimx
real, dimension (:), allocatable :: sr
integer, dimension (:), allocatable :: irs, jcs
mwpointer mxGetPr
mwpointer mxCreateDoubleMatrix
mwsize mxGetM, mxGetN, mxGetNzmax
mwpointer mxGetIr, mxGetJc
mwpointer temp_pr, spr
dimx(1) = mxGetM(prhs(1))
dimx(2) = mxGetN(prhs(1))
nzmax = mxGetNzmax(prhs(1))
allocate(sr(1:nzmax))
allocate(irs(1:nzmax))
allocate(jcs(1:(dimx(2)+1)))
temp_pr = mxGetPr(prhs(1))
call getreal(temp_pr,sr,nzmax)
temp_pr = mxGetJc(prhs(1))
plhs(1) = mxCreateDoubleMatrix(nzmax,1,0)
temp_pr = mxGetPr(plhs(1))
call putreal(sr, temp_pr, nzmax)
end
subroutine real8toreal(x, y, size)
integer size
real*8 x(size)
real y(size)
do 10 i=1,size
y(i)= x(i)
10 continue
return
end
subroutine getreal(pr,x,size)
mwpointer pr
integer size
real x(size)
real*8, dimension (:), allocatable :: temp
allocate(temp(1:size))
call mxCopyPtrToReal8(pr,temp,size)
call real8toreal(temp,x,size)
deallocate(temp)
return
end
subroutine putreal(x,pr,size)
mwpointer pr
integer size
real x(size)
real*8, dimension (:), allocatable :: temp
allocate(temp(1:size))
call realtoreal8(x,temp,size)
call mxCopyReal8ToPtr(temp,pr,size)
deallocate(temp)
return
end