MATLAB: Mex Fortran Gateway Function

fortrangateway functionmex

Hi,
Can someone help me write a mex gateway function for making a call to some fortran subroutines. The routines are written in fortran 77 and I'm using Microsoft Visual Studio 2005 with Intel Visual Fortran Compiler 10.1. The subroutines compile fine, but I'm having difficulty with the mex gateway function.
I'm trying to make a mex gateway function that will support a call to the following fortran subroutine:
subroutine crvfit(xfit,yfit,xfite,yfite,imode,yfit2,nptsf,delfac)
or
call crvfit(xfit,yfit,xfite,yfite,imode,yfit2,nptsf,delfac)
Inputs:
xfit, yfit – x and y data vectors to be fitted
xfite, yfite – the errors for the x and y vectors
imode = 1 (integer)
nptsf – the number of points to be fitted, i.e. length of xfit, yfit, xfite, and yfite
delfac = 500 (integer)
Outputs:
xfit, yfit
xfite, yfite
imode
yfit2 – temporary fit vector
nptsf2
delfac
So given these inputs, I want to return all of these outputs. The inputs will be passed to the function in MATLAB like:
[xfit, yfit, xfite, yfite, imode, yfit2, nptsf, delfac] = crvfit(xfit, yfit, xfite, yfite, imode, yfit2, nptsf, delfac)
I've tried to do this myself using example fortran mex gateway functions, but I'm very confused with using mex functions and having pointers to data types being passed through mex functions and returning them to MATLAB.
Thanks!

Best Answer

OK, here is some code you can try (CAUTION: UNTESTED). Uses the Fortran 95 MATLAB interface code from the FEX I mentioned in an earlier post (compile this first). Just call this file crvfit.f90 and then compile it like this:
mex crvfit.f90 MatlabAPImx.obj
If you run into problems let me know. It is horribly inefficient with regards to memory copying, but I am simply following your strict interface requirements.
#include "fintrf.h"
!---------------------------------------------------------------------
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
use MatlabAPImx
implicit none
!-ARG
mwPointer plhs(*), prhs(*)
integer*4 nlhs, nrhs
!-LOC
real(8), pointer :: xfit(:), yfit(:), xfite(:), yfite(:), yfit2(:)
integer imode, nptsf
real delfac
!-----
! insert some input argument checks here
xfit => fpGetPrCopy1(prhs(1))
yfit => fpGetPrCopy1(prhs(2))
xfite => fpGetPrCopy1(prhs(3))
yfite => fpGetPrCopy1(prhs(4))
imode = mxGetScalar(prhs(5))
yfit2 => fpGetPrCopy1(prhs(6))
nptsf = mxGetScalar(prhs(7))
delfac = mxGetScalar(prhs(8))
! check association status of xfit etc here
call crvfit(xfit,yfit,xfite,yfite,imode,yfit2,nptsf,delfac)
plhs(1) = mxArray(xfit(1:nptsf))
plhs(2) = mxArray(yfit(1:nptsf))
plhs(3) = mxArray(xfite(1:nptsf))
plhs(4) = mxArray(yfite(1:nptsf))
plhs(5) = mxArray(dble(imode))
plhs(6) = mxArray(yfit2(1:nptsf))
plhs(7) = mxArray(dble(nptsf))
plhs(8) = mxArray(dble(delfac))
call fpDeallocate(xfit)
call fpDeallocate(yfit)
call fpDeallocate(xfite)
call fpDeallocate(yfite)
call fpDeallocate(yfit2)
end subroutine mexFunction
subroutine crvfit(xfit,yfit,xfite,yfite,imode,yfit2,nptsf,delfac)
double precision xfit(*),yfit(*),xfite(*),yfite(*),yfit2(*)
integer imode
integer nptsf
real delfac
! etc
end subroutine