私はMatlabにFortranルーチンを追加する作業をしています(Mex関数経由)。私はMatlab、Mex、Fortranの間のリンクがどのように作られているかを理解するための簡単なプログラムを書こうとしています。WindowsでGFortranを使ってMatlabでMexファイルを作成する
私はx
とy
の値をとり、それらを合計して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
p * hs引数のデフォルトの整数型は十分ですか? 'mwPointer'sとして宣言してみてください。また、MEX Fortran APIを使ってこれを直接コンパイルしている場合(Cの介入なし)、 'c_f_pointer'は必要ないと思います。 – TroyHaskin
[Fortranソースファイルの基本例](https://in.mathworks.com/help/matlab/matlab_external/create-fortran-source-mex-file.html)が動作するかどうか確認できますか? –