2017-03-17 8 views
2

私はMatlabにFortranルーチンを追加する作業をしています(Mex関数経由)。私はMatlab、Mex、Fortranの間のリンクがどのように作られているかを理解するための簡単なプログラムを書こうとしています。WindowsでGFortranを使ってMatlabでMexファイルを作成する

私はxyの値をとり、それらを合計してzを出力する簡単なプログラムを書いた。しかし、コンパイル後にMatlabを実行しようとすると、Matlabは説明なしでクラッシュします。私がここで間違って何をしたのか?

! MEX FILE EXAMPLE 
! 


module yprime_mod ! test module for gnumex and g95 
    use mexinterface 
contains 
    subroutine yprime(x, y, z) ! subroutine yprime(z, t, y, error, x) 
    implicit none 
    double precision :: x, y, z 
    intent(in) :: x, y 
    intent(out) :: z 
    ! 
    z=x+y; 
    end subroutine yprime 
end module yprime_mod 

subroutine mexfunction(nlhs, plhs, nrhs, prhs) 
    use yprime_mod 
    implicit none 
    integer :: nlhs, nrhs, plhs(nlhs), prhs(nrhs) 
    double precision, pointer :: xp, yp, z 
    ! 
    if (nrhs /= 2) call mexerrmsgtxt('yprime requires two input arguments') 
    if (nlhs > 1) call mexerrmsgtxt('yprime requires one output argument') 
    call c_f_pointer(mxgetpr(prhs(1)), xp) ! assign pointers to parameters 
    call c_f_pointer(mxgetpr(prhs(2)), yp) 
    call c_f_pointer(mxgetpr(plhs(1)), z) 
    call yprime(xp, yp, z) 
end subroutine mexfunction 
+0

p * hs引数のデフォルトの整数型は十分ですか? 'mwPointer'sとして宣言してみてください。また、MEX Fortran APIを使ってこれを直接コンパイルしている場合(Cの介入なし)、 'c_f_pointer'は必要ないと思います。 – TroyHaskin

+0

[Fortranソースファイルの基本例](https://in.mathworks.com/help/matlab/matlab_external/create-fortran-source-mex-file.html)が動作するかどうか確認できますか? –

答えて

1

私はFortranを知らずに、基本的な例に従っています。

私はc_f_pointer(クラッシュに関連する可能性があります)を使用することについて何も知りません。
return引数の行列を作成するためにplhs(1) = mxCreateDoubleMatrix(mrows,ncols,0)を呼び出すことを忘れたため、コードがクラッシュする可能性が最も高いと思います。私はコンパイルのためにインテルコンパイラを使用、およびVisual Studioでコード(ステップバイステップ)をデバッグ

! MEX FILE EXAMPLE 
! 

#include "fintrf.h" 

module yprime_mod ! test module for gnumex and g95 
    !use mexinterface 
contains 
    subroutine yprime(x, y, z) ! subroutine yprime(z, t, y, error, x) 
    implicit none 
    double precision :: x, y, z 
    intent(in) :: x, y 
    intent(out) :: z 
    ! 
    z=x+y; 
    end subroutine yprime 
end module yprime_mod 
! 
!  
subroutine mexfunction(nlhs, plhs, nrhs, prhs) 
    use yprime_mod 
    implicit none 
    !integer :: nlhs, nrhs, plhs(nlhs), prhs(nrhs) 
    !double precision, pointer :: xp, yp, zp 
    !double precision x, y, z 

    !mexFunction arguments: 
    mwPointer plhs(*), prhs(*) 
    integer nlhs, nrhs 

    !Function declarations: 
    mwPointer mxGetPr 
    mwPointer mxCreateDoubleMatrix 
    mwPointer mxGetM, mxGetN 

    !Pointers to input/output mxArrays: 
    mwPointer xp, yp, zp 

    !Arguments for computational routine: 
    real*8 x, y, z 

    if (nrhs /= 2) call mexerrmsgtxt('yprime requires two input arguments') 
    if (nlhs > 1) call mexerrmsgtxt('yprime requires one output argument') 

    !To points to the input matrices data, use the mxGetPr function. 
    xp = mxGetPr(prhs(1)) 
    yp = mxGetPr(prhs(2)) 

    !Create Fortran arrays from the input arguments. 
    call mxCopyPtrToReal8(xp,x,1) 
    call mxCopyPtrToReal8(yp,y,1) 

    !Create scalar for the return argument. 
    plhs(1) = mxCreateDoubleMatrix(1,1,0) 

    !Use the mxGetPr function to assign the y_ptr argument to plhs(1). 
    zp = mxGetPr(plhs(1)) 

    !call c_f_pointer(mxgetpr(prhs(1)), xp) ! assign pointers to parameters 
    !call c_f_pointer(mxgetpr(prhs(2)), yp) 
    !call c_f_pointer(mxgetpr(plhs(1)), z) 

    !call yprime(xp, yp, zp) 

    !Perform Calculation 
    call yprime(x, y, z) 

    !Copy Results to Output Argument 
    call mxCopyReal8ToPtr(z,zp,1) 

end subroutine mexfunction 

:ここ

が変更されたコードです。
mexファイルが正常に動作しています...

+0

こんにちはアダム、私の答えについていくつかのフィードバックをお願いしますか? – Rotem

関連する問題