2013-07-24 98 views
2

我正在寫一個MEX文件,涉及從Matlab讀取稀疏矩陣,但每次運行程序時Matlab都會崩潰。然後我編寫一個測試文件,發現問題確切在mxGetIr命令中。我不知道爲什麼會發生這種情況。請幫助我...非常感謝!mxGetIr讓我的Matlab崩潰

我在Matlab 2013a下工作在Mac OS X 10.8.1。

#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 
+0

你確定你提供給函數的輸入確實是疏?使用['mxIsSparse'](http://www.mathworks.com/help/matlab/apiref/mxissparse.html)在內部檢查它。 – Shai

+0

謝謝!我檢查過,它是一個稀疏矩陣... – user2614035

回答

0

在檢查the documentation後,我認爲您可能需要以不同的方式調用它。

從那裏,我猜是這樣的:

mwPointer mxGetIr(pm) 
mwPointer pm