Showing results for 
Search instead for 
Did you mean: 

Fortran Compiler


I have been using Intel Parallel studio Fortran for the last ten years in order to create mex files for the Matlab environment. But now Intel is no longer distributing licences for Parallel Studio. I downloaded both oneAPI Basekit and HPCkit, but they are not recognized by Matlab.

Then I asked Matlab, but I have been informed that the only supported Fortran compiler is Intel Parallel Studio. I would be really grateful if you could help me in solving this problem and find a Matlab compatible version of Intel Fortran.


0 Kudos
11 Replies
Black Belt Retired Employee

You can configure Matlab to use the current compiler. Matlab uses a script (probably .bat file) that calls an Intel script, likely called ifortvars.bat or compilervars.bat to locate the compiler. You would need to change this to reference setvars.bat from the oneAPI compiler location:

C:\Program Files (x86)\Intel\oneAPI\setvars.bat

Black Belt

For the most common need I have for Mex files, i.e, a DLL for returning y = f(x) where x and y are vectors, the Matlab supplied script is an overkill. The following procedure is adequate, and works with any Fortran compiler that is binary ABI compatible.

1. Add the Matlab directory containing the MOD files for Mex creation to %INCLUDE%.

2. Add the Matlab directory containing the LIB files for Mex creation to %LIB%.

3. Build the DLL, telling the linker which Matlab libraries to read, and specifying the exported name with the expected case.

After building and testing the Mex file in Matlab, if you wish to change the source file(s) and rebuild the DLL, you have to issue the relevant CLEAR command in Matlab. Otherwise, the DLL file will be progected from being overwritten and the linker will fail.


Thank you for your reply, but I need mex files to integrate a couple of Fortran programs into a huge Matlab code. I am speaking about fortran programs that are more than 3000 lines long, and that I used to compile using the "mex" matlab command. At present Matlab seems not to recognize oneApi Base Toolkit and HPC Toolkit. Do you think that your suggestion will work also in my case?



Black Belt

I do not think that the size (a few thousand lines) of the Fortran source code is of any concern. If you can show the argument list (input variables, output variables, types), or provide the source file (zipped), I could comment on it.

That Matlab's Mex script cannot recognize OneAPI does not matter, because the procedure that I provided is for building the Mex DLL outside Matlab.


Thank you for you kind offer. The problem is that when I type "mex nameprog.f90" in the Matlab command line, I get 

Error using mex
No supported compiler was found. 

And when I type "mex -setup", I get 

Error using mex
No supported compiler was found.

Here below I report the argument list that used to work with Intel Parallel Studio. Thank you again for your help,


The argument list in the fortran code is:

        SUBROUTINE mexFunction(nlhs, plhs, nrhs, prhs)

        REAL*8 rNx, rNy, rcorr_funct 

        integer Nx, Ny, size

        REAL*8 theta_i_ini(6760000)     

        REAL*8 theta_sabs_mat(6760000)     

        REAL*8 phi_sabs_mat(6760000)       

        REAL*8 zetaloc(20280000)      

        REAL*8 thetailoc(6760000)      

        REAL*8 thetasloc(6760000)    

        REAL*8 phisloc(6760000)        

        REAL*8 mv_INI

        REAL*8 sds_ini

        REAL*8 L

        integer corr_funct

        REAL*8 beta2rad

        REAL*8 r2mat(6760000)             

        REAL*8 R0

        REAL*8 sigmaVV_cohe(6760000)     

        REAL*8 sigmaVV_inco(6760000)      !!! 251001)

        REAL*8 sigmaHH_cohe(6760000)      !!! 251001)

        REAL*8 sigmaHH_inco(6760000)      !!! 251001)

        CALL mxCopyPtrToReal8(mxGetPr(prhs(1)), rNx, &                                      mxGetNumberOfElements(prhs(1)))

        Nx = INT(rNx)

 CALL mxCopyPtrToReal8(mxGetPr(prhs(2)), rNy, &                                      mxGetNumberOfElements(prhs(2)))

        Ny = INT(rNy)

        size = Nx * Ny

 CALL mxCopyPtrToReal8(mxGetPr(prhs(3)), theta_i_ini,  &                                      mxGetNumberOfElements(prhs(3)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(4)), theta_sabs_mat,    &                                      mxGetNumberOfElements(prhs(4)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(5)), phi_sabs_mat,      &                                      mxGetNumberOfElements(prhs(5)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(6)), zetaloc,    &                                      mxGetNumberOfElements(prhs(6)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(7)), thetailoc,    &                                      mxGetNumberOfElements(prhs(7)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(8)), thetasloc,    &                                      mxGetNumberOfElements(prhs(8)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(9)), phisloc,      &                                     mxGetNumberOfElements(prhs(9)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(10)), mv_INI,       &                                     mxGetNumberOfElements(prhs(10)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(11)), sds_ini,      &                                     mxGetNumberOfElements(prhs(11)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(12)), L,            &                                     mxGetNumberOfElements(prhs(12)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(13)), rcorr_funct,  &                                     mxGetNumberOfElements(prhs(13)))

        corr_funct = INT(rcorr_funct)

 CALL mxCopyPtrToReal8(mxGetPr(prhs(14)), beta2rad,    &                                     mxGetNumberOfElements(prhs(14)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(15)), r2mat,       &                                     mxGetNumberOfElements(prhs(15)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(16)), R0,          &                                     mxGetNumberOfElements(prhs(16)))

 CALL mxCopyPtrToReal8(mxGetPr(prhs(17)), freq_ini,          &                                     mxGetNumberOfElements(prhs(17)))

         plhs(1) = mxCreateDoubleMatrix(Ny, Nx, 0)

         plhs(2) = mxCreateDoubleMatrix(Ny, Nx, 0)

         plhs(3) = mxCreateDoubleMatrix(Ny, Nx, 0)

         plhs(4) = mxCreateDoubleMatrix(Ny, Nx, 0)

CALL AIEM(freq_ini,Nx, Ny, theta_i_ini, theta_sabs_mat,phi_sabs_mat, &

            zetaloc,thetailoc, thetasloc,phisloc,  &

            mv_INI, sds_ini,  L, corr_funct, beta2rad, r2mat, R0, &

            sigmaVV_cohe, sigmaVV_inco, sigmaHH_cohe, sigmaHH_inco)

CALL mxCopyReal8ToPtr(sigmaVV_cohe, mxGetPr(plhs(1)),    &                        mxGetNumberOfElements(plhs(1)))

 CALL mxCopyReal8ToPtr(sigmaVV_inco, mxGetPr(plhs(2)),    &                        mxGetNumberOfElements(plhs(2)))

 CALL mxCopyReal8ToPtr(sigmaHH_cohe, mxGetPr(plhs(3)),    &                        mxGetNumberOfElements(plhs(3)))

 CALL mxCopyReal8ToPtr(sigmaHH_inco, mxGetPr(plhs(4)),    &                        mxGetNumberOfElements(plhs(4)))




Valued Contributor I

1. I would wait for @mecej4 to help, but if you are impatient. 

2. mex is looking for the ifort compiler at a fixed location, you will need to trick mex into using the more up to date one -- interesting little problem about redirection -- I do not have several hours to play with it and mex is compiled so one is sort of stuck a bit.  

3. It is a challenge but an interesting one.  

4. I really dislike programs like matlab, they are just a fancy Front end for Fortran or C. 

Valued Contributor I

MEX   Compile MEX-function

       mex [options ...] file [files ...]

       MEX compiles and links source files into a shared library called a 
       MEX-file, executable from within MATLAB. It also builds executable
       files for standalone MATLAB engine and MAT-file applications.
   Command Line Options Available on All Platforms:
           Compile only. Creates an object file but not a MEX-file.
       -client engine
           Build standalone MATLAB engine or MAT-file application.
           Build a MEX-file using the MATLAB Version 7.2 array-handling
           API, which limits arrays to 2^31-1 elements.
           Define a symbol name to the C preprocessor. Equivalent to a
           "#define <name>" directive in the source. Do not add a space
           after this switch.
           Define a symbol name and value to the C preprocessor. Equivalent
           to a "#define <name> <value>" directive in the source. Do not
           add a space after this switch.
       -f <optionsfile>
           For advanced users. Specify location and name of the MEX
           configuration file to use. Overrides MEX's default compiler
           selection mechanism.
           Create a MEX-file containing additional symbolic information for
           use in debugging. This option disables MEX's default behavior of
           optimizing built object code (see the -O option).
           Display this message.
           Add <pathname> to the list of directories to search for #include
           files. Do not add a space after this switch.
           Link with object library. On Windows, <name> expands to
           "<name>.lib" or "lib<name>.lib". On Linux, to "lib<name>.so".
           On Mac, to "lib<name>.dylib". Do not add a space after this
           Add <folder> to the list of folders to search for
           libraries specified with the -l option. Do not add a space 
           after this switch.
           Build a MEX-file using the MATLAB large-array-handling API.
           This API can handle arrays with more than 2^31-1 elements when
           compiled on 64-bit platforms.
           Build a MEX-file using the MATLAB large-array-handling, and
           graphics-objects APIs. This option is available on 64-bit
           platforms. The API can handle arrays with more than
           2^31-1 elements. -R2017b is the default option.
           Build a MEX-file using the MATLAB large-array-handling,
           graphics-objects, and interleaved-complex APIs. This option
           is available on 64-bit platforms. The API can handle arrays
           with more than 2^31-1 elements, and supports type-safe and
           interleaved complex data access.
           No execute mode. Display commands that MEX would otherwise
           have executed, but do not actually execute any of them.
           Optimize the object code. Optimization is enabled by default and
           by including this option on the command line. If the -g option
           appears without the -O option, optimization is disabled.
       -outdir <dirname>
           Place all output files in folder <dirname>.
       -output <resultname>
           Create MEX-file named <resultname>. The appropriate MEX-file
           extension is automatically appended. Overrides MEX's default
           MEX-file naming mechanism.
       -setup <lang>
           Change the default compiler to build <lang> language MEX-files.
           When this option is specified, no other command line 
           input is accepted.
           Suppress informational messages. The mex function still reports
           errors and warnings, even when you specify -silent.
           Remove any initial definition of the C preprocessor symbol
           <name>. (Inverse of the -D option.) Do not add a space after
           this switch.
           Verbose mode. Display the values for important internal
           variables after all command line arguments are considered.
           Displays each compile step and final link step fully evaluated.
           Override default setting for variable <name>. This option is 
           processed after all command line arguments are considered. 

   Command Line Options Available Only on Windows Platforms:
           Include contents of the text file <rspfile> as command line
           arguments to MEX.

   For more information, see


This is from the mexhelp file it tells you to use the f switch to override the default compiler config. 

You will need to search for the compiler config - it is not a bat file i could find 

Valued Contributor I

Do Not Use mex -f optionsfile Syntax

The mex command -f option to specify a build configuration file will be removed in a future release. Instead, use the workflows described in this topic for specifying a compiler.


I have matlab 2020b installed with an approved Fortran compiler and it cannot find it .. 

Black Belt

Leila, the source code that you posted is not usable without a lot of effort. Many lines have been unwrapped, and the code is not valid Fortran free form source anymore.

Please do not place code in the body of your reply. Either use the code insertion tool (click on "...", then "</>", then paste the code into the box) or, which is the best, zip up the source code, upload the zip file and attach the zip to your response.

Which version of Matlab do you use?

This is what you need to do to build a 64-bit Mex file (DLL):

  • open an Intel Fortran 64-bit command window from the Start Menu.
  • Add the Matlab extern\include directory path to %INCLUDE%.
  • Add the Matlab extern\lib\win64\microsoft to %LIB%.
  • Add the line #include <fintrf.h> to your source file that contains the mexFunction subroutine.
  • Compile the file using IFORT with the options /LD /c /fpp
  • Compile the remaining Fortran source files (for instance, the file containing subroutine AIEM).
  • Link all the OBJ files with the command below. 
  • Test the Mex file from Matlab

Here is the linking command:



ifort /LD leila.obj aiem.obj libmx.lib /Feleila.mexw64 /link /export:MEXFUNCTION






I am really thankful for your advise. But it's really weird that until one month ago I just needed one line to include an old fortran program into a Matlab code, and now I have to write all this stuff.

Again, I thank you very much, but I think that Intel and Matlab should solve this conflict and offer their customers a convenient solution for all users, not only advanced ones.

Black Belt

Mathworks is "downstream" from Intel which, in term, is downstream from Microsoft/Apple. 

Often, Microsoft/Apple change the OS or the development framework (Visual Studio; Xcode) with no notice to Intel and it takes time for Intel to catch up. Similarly, I suspect, when Intel or GNU change the Fortran compiler, Mathworks may take time to catch up, or even to notice that updates are required.

Now that Intel compilers are free to use and do not require licenses, I expect that such issues will become more common. You and I are now "community" rather than "customers". With expensive software such as Matlab, Abaqus, etc., an additional obstacle is that most of us "community" members have no access at all to that software, or may only have an old or cut-down version. For instance, I have little incentive to find out how the current version of Matlab differs from the 2007 or 2012 versions that I have licenses for.