User`s manual

Fortran MEX-Files
3-5
The Pointer Concept
The MATLAB API works with a unique data type, the mxArray. Because there
is no way to create a new data type in Fortran, MATLAB passes a special
identifier, called a pointer, to a Fortran program. You can get information
about an
mxArray by passing this pointer to various API functions called
Access Routines. These access routines allow you to get a native Fortran data
type containing exactly the information you want, i.e., the size of the
mxArray,
whether or not it is a string, or its data contents.
There are several implications when using pointers in Fortran:
The
%val construct.
If your Fortran compiler supports the
%val construct, then there is one type
of pointer you can use without requiring an access routine, namely a pointer
to data (i.e., the pointer returned by
mxGetPr or mxGetPi). You can use %val
to pass this pointer’s contents to a subroutine, where it is declared as a
Fortran double-precision matrix.
If your Fortran compiler does not support the
%val construct, you must use
the
mxCopy__ routines (e.g., mxCopyPtrToReal8) to access the contents of the
pointer. For more information about the
%val construct and an example, see
the section, “The %val Construct” on page 3-8.
Variable declarations.
To use pointers properly, you must declare them to be the correct size. On
DEC Alpha machines, all pointers should be declared as
integer*8. On all
other platforms, pointers should be declared as
integer*4.
If your Fortran compiler supports preprocessing with the C preprocessor, you
can use the preprocessing stage to map pointers to the appropriate
declaration. In UNIX, see the examples ending with
.F in the examples
directory for a possible approach.
Caution Declaring a pointer to be the incorrect size may cause your program
to crash.