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)가 작동하는지 확인할 수 있습니까? –