Building Mex file with Fortan

I am trying to understand how to build mex files. I made following test code It compiles using the Visual Fortran Compiler 2011 but when i try to call it matlab shuts down with the message segmentation violation detected. Does anyone sees what goes wrong?
if true
#include <fintrf.h>
C
C Gateway routine
C
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
C mexFunction arguments:
MWPOINTER PLHS(*), PRHS(*)
MWPOINTER NLHS, NRHS
mwPointer mxGetPr,mxGetPi
mwPointer mxCreateDoubleMatrix,mxCreateNumericArray
mwSize mxGetM, mxGetN
MWPOINTER YPP
REAL *8 RYPP(1)
YPP = MXGETPR(PLHS(1))
RYPP(1) = 10
CALL MXCOPYREAL8TOPTR(RYPP, YPP, 1)
RETURN
END
end

 Accepted Answer

Your main problem is that you need to create the output array PLHS(1) before you access it. Also your NLHS and NRHS types are wrong. Try this:
#include <fintrf.h>
C
C Gateway routine
C
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
implicit none
C mexFunction arguments:
MWPOINTER PLHS(*), PRHS(*)
INTEGER NLHS, NRHS
mwPointer, external :: mxGetPr, mxGetPi
mwPointer, external :: mxCreateDoubleMatrix, mxCreateNumericArray
mwSize, external :: mxGetM, mxGetN
MWPOINTER YPP
mwSize M, N
integer*4 ComplexFlag
REAL *8 RYPP(1)
RYPP(1) = 10
M = 1
N = 1
ComplexFlag = 0
PLHS(1) = mxCreateDoubleMatrix( M, N, ComplexFlag )
YPP = MXGETPR(PLHS(1))
CALL MXCOPYREAL8TOPTR(RYPP, YPP, M*N)
RETURN
END

More Answers (0)

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

Asked:

on 6 Nov 2013

Answered:

on 6 Nov 2013

Community Treasure Hunt

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

Start Hunting!