Fortran 95 Interface to MATLAB API with extras!

This is the one you have been waiting for, a clean interface using assumed shape Fortran pointers.
2.9K Downloads
Updated 11 Dec 2011

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:
MatlabAPImx.f
MatlabAPImex.f
MatlabAPIeng.f
MatlabAPImat.f

Mex Test routines:
MatlabAPI_real.f
MatlabAPI_complex.f
MatlabAPI_getset.f
MatlabAPI_matfile.f
MatlabAPI_implicit.f

Engine Test routine:
MatlabAPI_engine.f

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:
MatlabAPI_build_cfv61.m
MatlabAPI_build_cfv66.m
MatlabAPI_build_intelf90.m
MatlabAPI_build_intelf90msvs2003.m
MatlabAPI_build_intelf90msvs2005.m
MatlabAPI_build_intelf91msvs2005.m
MatlabAPI_build_intelf10msvs2005.m

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
:
Etc

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.

Testing:

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 (2024). Fortran 95 Interface to MATLAB API with extras! (https://www.mathworks.com/matlabcentral/fileexchange/25934-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
Categories
Find more on Fortran with MATLAB in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.3.0.0

Fixed hard-coded integer constants in arguments to be mwSize instead (mainly affects 64-bit compiler support). Added 0 argument functionality to fpReshape. Added explicit checks to disallow 0-sized targets.

1.2.0.0

Fixed another typo in the pdf file.

1.1.0.0

Changed default address function from %LOC( ) to LOC( ). Added a new test routine MatlabAPI_implicit. Fixed some typos in the pdf file.

1.0.0.0