Matlab Fortran MEX setup

Demonstrate that Matlab MEX Fortran compiler wrapper is working on your system with the following commands. Ensure you are using a supported compiler with Matlab as the supported compilers are distinct across operating systems. These commands are all from within Matlab.

Setup Matlab Fortran compiler

Configure Matlab for Fortran compiler:

mex -setup -v FORTRAN

Verify configuration by

mex.getCompilerConfigurations('FORTRAN')

Example Fortran MEX

  1. copy over an example Fortran program.

    copyfile(fullfile(matlabroot,'extern','examples','refbook','timestwo.F'), pwd)
    copyfile(fullfile(matlabroot,'extern','include','fintrf.h'), pwd)
    
  2. compile

    mex timestwo.F
    
  3. use the compiled code

    timestwo(3)
    

    ans = 6.0

Notes

You may need additional Matlab compiler configuration if you get compiler errors like:

... Looking for folder 'libgfortranbegin.a' ...No.
Did not find installed compiler 'gfortran'.
Error using mex
No supported compiler or SDK was found. For options, visit
https://www.mathworks.com/support/compilers/current_release/.

Windows MEX Fortran

Matlab and Fortran on Windows can use Intel oneAPI to compile Fortran.

Alternatives

GNU Octave and Gfortran MEX are available on Windows, MacOS and Linux.