image thumbnail

Fortran 95 Interface to MATLAB API with extras!

version (157 KB) by James Tursa
This is the one you have been waiting for, a clean interface using assumed shape Fortran pointers.


Updated Sun, 11 Dec 2011 02:53:58 +0000

View License

Finally, a clean Fortran 95 interface to the MATLAB API routines, with extras! A complete interface to existing MATLAB API functions (eng, mat, mex, mx), as well as dozens of powerful new routines that work with assumed shape Fortran Pointers. Functions are available to, in one line:

- Get shaped pointers into the data area of an mxArray
- Get shaped pointers that point to a copy of the data area of an mxArray
- Reshape a pointer or an array *without* copying
- Allocate Fortran pointers using MATLAB’s allocation functions
- Get lists of workspace variable names or mat file variable names

You will never call mxGetPr, mxGetPi, mxGetDimensions, etc. again once you see the Fortran pointer routines I have included in this package! See the enclosed pdf file for a description of all the available functions in this package.

There are four module routines, five mex test routines, and one engine test routine. The files in the MatlabAPI suite are (should be compiled in the order shown):

Module Code:

Mex Test routines:

Engine Test routine:

The test routines do not take any inputs. Some give no outputs and some give one output. They can be compiled with the build scripts included on a 32-bit WinXp system with Intel or Compaq Fortran:

Build Scripts:

Before you run the build scripts, you must select a Fortran compiler:

> mex –setup
(then select a Fortran 95 compiler)

Running the test programs:

MatlabAPI_real, MatlabAPI_complex, MatlabAPI_matfile, and MatlabAPI_implicit are one-line runs with no inputs:

> MatlabAPI_real
> MatlabAPI_complex
> MatlabAPI_matfile
> MatlabAPI_implicit

MatlabAPI_getset also has no inputs, but is designed to be called multiple times (be sure to have the figure uncovered to see the results … you may need to shrink the MATLAB window):

> MatlabAPI_getset
> MatlabAPI_getset
> MatlabAPI_getset
> MatlabAPI_getset

MatlabAPI_engine is the engine version of MatlabAPI_getset. It can be run with the bang operator:

> !MatlabAPI_engine

Again, be sure to have the figure uncovered to see the results.


I have only tested this suite on 32-bit Windows XP with Intel Fortran 9.1 and 10, and Compaq Fortran 6.1, using various MATLAB versions R2006b – R2009b. I would appreciate it if others with different OS/Machine/Compiler setups can try these out and report back on the results, or maybe send me modified build scripts that worked for their setup. Ultimately I would like this suite to evolve into a package that will work across multiple platforms with multiple compilers.

Future upgrades planned:

- Bug fixes, of course.
- Additional Fortran pointer routines for various functions.
- More extensive test routines.
- Expanded documentation and examples.
- Support for linux and mac machines. But this will depend on other users willing to supply this code to me, since I do not have access to these machines for development or testing.
- Support for 64-bit machines. But this will depend on other users willing to supply this code to me, since I do not have access to these machines for development or testing.

Future upgrades that I *may* consider depending on my time availability and the popularity of MatlabAPI:

- Support for other classes (e.g., single, int8, int16, int32, int64, logical). The current set of routines only work with double class variables.
- More Fortran Pointer routines if I can think of some more ideas on how to use them effectively.
- Support for older versions of MATLAB, particularly versions prior to 7. My guess is that some of the API functions I used might not be compatible. Again, this will depend on other users willing to help modify the code, since I do not have access to these older versions of MATLAB for development or testing.

Feel free to post items of general interest to other users (bug reports, performance data, questions about usage or optimizations, etc) directly on the FEX of course. But if you have modified the code for your version of MATLAB (older version, non-PC machine, non-supported Fortran compiler, etc.) please feel free to contact me directly and I will try to incorporate them into future MatlabAPI upgrades. You can reach me at

a#lassyguy%ho$mail_com (replace # with k, % with @, $ with t, _ with .)

James Tursa

Cite As

James Tursa (2022). Fortran 95 Interface to MATLAB API with extras! (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!