I wrote a mex file to call a Fortran subroutine from MATLAB, and access violation occurs. Probably the reason for this error is that I make some mistakes when trying to passing the output variable y from Fortran to MATLAB. I insert mxPrintf into the program and can see where it stops, but I do not know how to correct it. I rewrote the code with a very simple subroutine, built it, and the problem persists when running it. I am sorry for asking such a basic question, and thank you in advance.
#include "fintrf.h"
subroutine mexFunction(nlhs,plhs,nrhs,prhs)
implicit none
mwPointer plhs(*),prhs(*)
integer nlhs,nrhs
mwPointer mxCreateNumericArray,mxGetNumberOfElements,mxGetPr,x_pr
mwPointer y_pr
mwSize mxGetNumberOfDimensions,dims(2)
integer mxClassIDFromClassName,classid
double precision x(2),y(2)
call mexPrintf("a")
dims(1)=2
dims(2)=1
call mexPrintf("b")
classid=mxClassIdFromClassName('double')
call mexPrintf("c")
plhs(1)=mxCreateNumericArray(2,dims,classid,0)
call mexPrintf("d")
x_pr=mxGetPr(prhs(1))
call mexPrintf("e")
y_pr=mxGetPr(plhs(1))
call mexPrintf("f")
call mxCopyPtrToReal8(x_pr,x,mxGetNumberOfElements(x_pr))
call mexPrintf("g")
call test(x,y)
call mexPrintf("h") ! Write after this line, the program stops.
call mxCopyReal8ToPtr(y,y_pr,mxGetNumberOfElements(y_pr))
call mexPrintf("i")
return
end
subroutine test(x,y)
implicit none
double precision x(2),y(2)
y(1)=1
y(2)=2
return
end

댓글 수: 1

Takaaki Itoga
Takaaki Itoga 2016년 1월 22일
I solve the problem by myself. I should have put prhs(1) and plhs(1) in the parentheses following mxGetNumberOfElements.

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2016년 1월 25일

0 개 추천

Note that the signature for mxCopyPtrToReal8 is as follows:
subroutine mxCopyPtrToReal8(px, y, n)
mwPointer px
real*8 y(n)
mwSize n
And likewise:
subroutine mxCopyReal8ToPtr(y, px, n)
real*8 y(n)
mwPointer px
mwSize n
So the last agrument is mwSize, not mwPointer. This may cause problems for you in the future if you are on a system where mwSize does not match mwPointer. You might consider writing code that forces the correct type. E.g.,
mwSize n
:
n = mxGetNumberOfElements(prhs(1))
call mxCopyPtrToReal8(x_pr,x,n)
:
n = mxGetNumberOfElements(plhs(1))
call mxCopyReal8ToPtr(y,y_pr,n)
And in Fortran, it is usually a good idea to always use variables for arguments to the API routines so you can force the correct type being passed. E.g., I never pass literal integers in arguments, I always create a variable of the desired type, set it to the desired value, and then pass that as the argument.

댓글 수: 1

Takaaki Itoga
Takaaki Itoga 2016년 1월 26일
Thank you very much for your answer. I noticed the problem you point out after submitting my question. I will follow your advice to avoid any possible future problems.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

질문:

2016년 1월 22일

댓글:

2016년 1월 26일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by