MATLAB Application Program Interface Reference Help Desk

Passing Pointers in Fortran

Description

The MATLAB API works with a unique data type, the mxArray. Because there is no way to create a new data type in Fortran 77, 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 -- 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:

  1. The %VAL construct.
  2. 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 array.
  3. 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.
  4. Variable declarations.
  5. To use pointers properly, you must declare them to be the correct size. On DEC Alpha and 64-bit SGI machines, all pointers should be declared as integer*8. On all other platforms, pointers should be declared as integer*4.
  6. 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.
  7. Note: Declaring a pointer to be the incorrect size can cause your program to crash.


[ Previous | Help Desk | Next ]