Mex Fortran Gateway Function

9 vues (au cours des 30 derniers jours)
Aaron
Aaron le 10 Juin 2011
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!
  2 commentaires
Jan
Jan le 11 Juin 2011
Although we could try to create the mexFunction for you, but as long as you are too confused by the details, you have no chance to adjust any details. Therefire I assume the most efficient method to solve your problem is to learn how to use pointers. Have you inspected the examples shipped with Matlab?
Aaron
Aaron le 12 Juin 2011
Yes, I have experimented with the examples shipped with Matlab. I have a good understanding of pointers in other languages, so I will not be confused with adjusting the dynamics of the code.
If you think I might be confused, comments in your answer never hurt :)

Connectez-vous pour commenter.

Réponse acceptée

James Tursa
James Tursa le 14 Juin 2011
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
  1 commentaire
Aaron
Aaron le 17 Juin 2011
Thank you so much for your help! I'm sorry it took me so long to respond. After debugging some of the crvfit and lower level fortran 77 routines, this interface worked wonderfully. I modified it slightly, but it was general enough to easily adapt. Thank you for your help! A+!

Connectez-vous pour commenter.

Plus de réponses (1)

James Tursa
James Tursa le 11 Juin 2011
Please provide the exact interface for this routine:
subroutine crvfit(xfit,yfit,xfite,yfite,imode,yfit2,nptsf,delfac)
i.e., give the type, rank, and size specifications for each of the arguments as they are listed in the subroutine. Then it will likely not be too hard to create the mex gateway for this using my Fortran 95 Interface package from the FEX:
I can help you once I know the exact interface for the routine.
  5 commentaires
James Tursa
James Tursa le 13 Juin 2011
Sorry if I keep pestering you with questions, but the interface code will depend on the answers you give. My interpretation of what you have said thus far is that xfit, yfit, xfite, yfite, and yfit2 are all pre-populated vectors of size nptsf on input, but that the crvfit routine will return a *different* value of nptsf (presumably *less* than the input value of nptsf?) and that you want the returned size of these vectors to be this new value of nptsf. Is that correct?
Aaron
Aaron le 13 Juin 2011
Exactly! Yes, this is correct.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Fortran with MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by