Hi. I have written and compiled a mex file from a Fortran source code. However, I have problems running it as Matlab crashes whenever I run the mex file. Can anybody find out where is the problem?
My fortran source code is as follows.
#include "fintrf.h"
C======================================================================
#if 0
C
C add.F
C .F file needs to be preprocessed to generate .for equivalent
C
#endif
C
C add.f
C
C Adds two integers
C
C======================================================================
C Gateway routine
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
C Declarations
implicit none
C mexFunction arguments:
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
C Pointers to input/output mxArrays:
mwPointer a, b
mwPointer c
C Function Declerations
mwPointer mxGetData
a = mxGetData(prhs(1))
b = mxGetData(prhs(2))
c = mxGetData(plhs(1))
C Call the computational subroutine.
call add(%val(a), %val(b), %val(c))
return
end
C-----------------------------------------------------------------------
C Computational subroutine
subroutine add(a, b, c)
integer*8 a, b, c
c = a + b
return
end

 채택된 답변

James Tursa
James Tursa 2014년 6월 3일
편집: James Tursa 2014년 6월 3일

2 개 추천

The main problem is that you, the programmer, need to create the output mxArray plhs(1). When you run the mex routine the only mxArray variables you start with are the prhs(etc) variables. At that point in the code, prhs(1) is unallocated. MATLAB does not create the plhs(etc) outputs for you since it does not know what the output is supposed to be ... that is your job as the programmer. Also, you should check that the inputs are int64 type before you use them that way. E.g.,
C Gateway routine
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
C Declarations
implicit none
C mexFunction arguments:
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
C Pointers to input/output mxArrays:
mwPointer a, b
mwPointer c
C Function Declerations
mwPointer mxGetData
integer*4, external :: mxClassIDFromClassName
integer*4, external :: mxIsInt64
mwPointer, external :: mxGetNumberOfElements
mwPointer, external :: mxCreateNumericMatrix
mwSize :: one = 1
integer*4 :: mxREAL = 0
integer*4 classid
if( nrhs /= 2 .or. nlhs > 1 ) then
call mexErrMsgTxt("Need 2 inputs and at most 1 output")
endif
if( mxIsInt64(prhs(1))==0 .or. mxIsInt64(prhs(2))==0 ) then
call mexErrMsgTxt("Inputs must be int64 class")
endif
if( mxGetNumberOfElements(prhs(1)) /= 1 .or.
& mxGetNumberOfElements(prhs(2)) /= 1 ) then
call mexErrMsgTxt("Inputs must be scalar")
endif
classid = mxClassIDFromClassName("int64")
plhs(1) = mxCreateNumericMatrix(one,one,classid,mxREAL)
a = mxGetData(prhs(1))
b = mxGetData(prhs(2))
c = mxGetData(plhs(1))
C Call the computational subroutine.
call add(%val(a), %val(b), %val(c))
return
end
C-----------------------------------------------------------------------
C Computational subroutine
subroutine add(a, b, c)
integer*8 a, b, c
c = a + b
return
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 COM Component Integration에 대해 자세히 알아보기

태그

질문:

2014년 6월 3일

편집:

2014년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by