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


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


    ans = 6.0


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

Windows MEX Fortran

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


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