Matlab Fortran MEX selftest

These commands are all from within Matlab. They demonstrate that Matlab (or GNU Octave) MEX Fortran compiler wrapper is working on your system.

  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. in Matlab or Octave:

    mex timestwo.F
  3. in Matlab or Octave:


    ans = 6.0

Matlab MEX Fortran issues

mex -setup -v FORTRAN


If you get error

… 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

For Windows, even through R2019a, MinGW Gfortran is not supported. The solution for Matlab and Fortran on Windows is to use Intel Fortran compiler, which is not free.

Consider using GNU Octave and Gfortran on Windows and Mac. Gfortran and Matlab MEX are fine on Linux.