How to compile a Fortran subroutine by using the MEX engine
Show older comments
Hi!
I have been trying to use a Matlab function (interp2) in Fortran using the Mex engine.
I have adapted the Fengdemo.F to suit my needs and the test cases work.
However, it is supposed to be part of a larger Fortran project with input coming from the rest of the program.
Therefore, I need the function as a subroutine or such, but mex does not let me compile it as anything other than a main() program, which inherentely does not accept input.
I have tried defining it as a subroutine, as well as a module.
Any ideas on what I might do wrong or how to get around this problem?
I find it hard to imagine the mex engine is only able to run main() programs as this would render this function more or less useless.
I am attaching a minimal example based on Fengdemo.F which is now a subroutine and just supposed to square an input.
Any hints are highly appreciated!
Kind regards
Lars
module InterpMatlab_mod
contains
#include "fintrf.h"
! Interpolation
#if 0
!
! fengdemo.F
! .F file need to be preprocessed to generate .for equivalent
!
#endif
!
! fengdemo.f
!
! This is a simple program that illustrates how to call the MATLAB
! Engine functions from a FORTRAN program.
!
! Copyright 1984-2018 The MathWorks, Inc.
!======================================================================
!
subroutine InterpolMatlab(x)
! Declarations
implicit none
mwPointer engOpen, engGetVariable, mxCreateDoubleMatrix
#if MX_HAS_INTERLEAVED_COMPLEX
mwPointer mxGetDoubles
#else
mwPointer mxGetPr
#endif
mwPointer x_point, xOut_point
integer engPutVariable, engEvalString, engClose
mwSize M
parameter(M=1)
integer, PARAMETER :: sp = KIND(1.0D0)
!----------------------------------------------------------------
! Input
integer :: x
! Output
integer :: x_square
!--------------------------------------------------------------------------------------------------!
ep = engOpen('matlab ')
if (ep .eq. 0) then
write(6,*) 'Can''t start MATLAB engine'
stop
endif
write(6,*) 'started MATLAB engine'
! Place the variables into the MATLAB workspace
x_point = mxCreateDoubleMatrix(M, M, 0)
#if MX_HAS_INTERLEAVED_COMPLEX
call mxCopyReal8ToPtr(x, mxGetDoubles(x_point), M)
#else
call mxCopyReal8ToPtr(x, mxGetPr(x_point),M)
#endif
status = engPutVariable(ep, 'x_point', x_point)
if (status .ne. 0) then
write(6,*) 'engPutVariable failed'
stop
endif
! Calculations in Matlab
if (engEvalString(ep, 'x_out=x^2') .ne. 0) then
write(6,*) 'engEvalString failed'
stop
endif
xOut_point = engGetVariable(ep, 'x_out')
write(6,*) 'Got pointer'
#if MX_HAS_INTERLEAVED_COMPLEX
call mxCopyPtrToReal8(mxGetDoubles(xOut_point), x_square, 1)
#else
call mxCopyPtrToReal8(mxGetPr(xOut_point), x_square, 1)
#endif
write(6,*) 'Result=', x_square
print *, 'Type 0 <return> to Exit'
print *, 'Type 1 <return> to continue'
read(*,*) temp
if (temp.eq.0) then
print *, 'EXIT!'
status = engClose(ep)
if (status .ne. 0) then
write(6,*) 'engClose failed'
endif
stop
end if
call mxDestroyArray(x_point)
call mxDestroyArray(xOut_point)
status = engClose(ep)
if (status .ne. 0) then
write(6,*) 'engClose failed'
stop
endif
stop
end
end module InterpMatlab_mod
4 Comments
James Tursa
on 8 Mar 2022
Edited: James Tursa
on 8 Mar 2022
We need you to post a small sample of code that reproduces the problem. Then we can steer you in the right direction. Do you want to call this Fortran code from MATLAB, i.e. getting inputs from MATLAB and perhaps returning outputs to MATLAB? That is the general outline of a mex function using the subroutine mexFunction( ) interface for the input/output.
Lars Schmitz
on 9 Mar 2022
Edited: Lars Schmitz
on 9 Mar 2022
Benjamin Thompson
on 9 Mar 2022
Probably not worth the effort. Can you find or code up a 2D interpolation algorithm in Fortran directly? Otherwise you can probably call C functions from Fortran. See "Numerical Recipies in C" for possible sample implementations, or I suppose you can use MATLAB Coder to generate the C code, and compile it into a C library that Fortran could use. Seems like a lot of work for a fairly simple algorithm though.
Lars Schmitz
on 16 Mar 2022
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!