Another common practice is to allocate memory using the MATLAB memory manager (mxCalloc) and pass this to another Fortran routine by value to create temporary arrays for copying, for example, integers to and from MATLAB arrays.
The g77 compiler does have the %VAL construct and this is the method used.
In Fortran 95 you can declare a pointer which can then be passed to another Fortran routine that is expecting an array argument. This means that the %VAL construct is not needed. Note, though, that the compiler may generate warnings because of the mismatch in type and attributes between actual and dummy arguments.
On Linux, the NAGWare f95 Compiler may be used to generate MEX files that make full use of dynamic allocation of memory. But, note that the "-mismatch_all" option must be specified to downgrade the argument type mismatch error to a warning.
The changes to the standard example are marked in the code with comments.
subroutine mexfunction(nlhs, plhs, nrhs, prhs)
integer nlhs, nrhs
integer plhs(*), prhs(*)
! New interface block for pointer function mxGetPr.
interface
function mxgetpr(pm)
integer,pointer :: mxgetpr
integer :: pm
end function mxgetpr
end interface
integer m_in, n_in
! New declaration for the pointers.
integer, pointer :: pr_in, pr_out
m_in = mxGetM(prhs(1))
n_in = mxGetN(prhs(1))
! Use pointer assignment
pr_in => mxgetpr(prhs(1))
plhs(1) = mxcreatefull(m_in,n_in,0)
! Use pointer assignment
pr_out => mxgetpr(plhs(1))
! pass pr_in and pr_out straight to dbl_mat
call dbl_mat(pr_out,pr_in,m_in,n_in)
end
subroutine dbl_mat(out_mat,in_mat,m,n)
integer m, n
double precision out_mat(m,n), in_mat(m,n)
! We don't need DO loops in Fortran 95!
out_mat = 2*in_mat
end
FC='f95'
FFLAGS='-Wc,-fPIC -mismatch_all -dcfuns -w=all'
FLIBS='/usr/local/lib/f95/quickfit.o /usr/local/lib/f95/libf96.a'
LD='gcc'
Note, the FLIBS directory shown above may be different on your system.
FC='f95'
FFLAGS='-Wc,-fPIC -mismatch_all -dcfuns -w=all'
FLIBS=''
LD="$COMPILER"
NWF95='-ldarg -Wl,-export-dynamic'
TMWFLAGS="-Wl,-shared -ldarg -Wl,--version-script,$TMW_ROOT/extern/lib/$Arch/$MAPFILE"
LDFLAGS="$NWF95 $TMWFLAGS"
Note, the -ldarg option is equivalent to "-Wl," but allows
commas in its argument.
© The Numerical Algorithms Group Ltd, Oxford UK. 2001-2004