User`s manual

3 Creating Fortran MEX-Files
3-8
When a MEX-file completes its task, it returns control to MATLAB. Any
MATLAB arrays that are created by the MEX-file that are not returned to
MATLAB through the left-hand side arguments are automatically destroyed.
The %val Construct
The %val construct is supported by most, but not all, Fortran compilers.
DIGITAL Visual Fortran does support the construct.
%val causes the value of
the variable, rather than the address of the variable, to be passed to the
subroutine. If you are using a Fortran compiler that does not support the
%val
construct, you must copy the array values into a temporary true Fortran array
using special routines. For example, consider a gateway routine that calls its
computational routine,
yprime, by
call yprime(%val(yp), %val(t), %val(y))
If your Fortran compiler does not support the %val construct, you would replace
the call to the computational subroutine with
C Copy array pointers to local arrays.
call mxCopyPtrToReal8(t, tr, 1)
call mxCopyPtrToReal8(y, yr, 4)
C
C Call the computational subroutine.
call yprime(ypr, tr, yr)
C
C Copy local array to output array pointer.
call mxCopyReal8ToPtr(ypr, yp, 4)
You must also add the following declaration line to the top of the gateway
routine.
real*8 ypr(4), tr, yr(4)
Note that if you use mxCopyPtrToReal8 or any of the other mxCopy__ routines,
the size of the arrays declared in the Fortran gateway routine must be greater
than or equal to the size of the inputs to the MEX-file coming in from MATLAB.
Otherwise
mxCopyPtrToReal8 will not work correctly.