MATLAB_CALLS_F90
Examples of MATLAB calling FORTRAN90 functions


MATLAB_CALLS_F90 is a directory of MATLAB programs which illustrate how a MATLAB program can call a FORTRAN90 function, passing data to the function, and receiving results from the FORTRAN90 function.

Of course, a MATLAB call normally looks something like

[ out1, out2 ] = function_name ( in1, in2, in3 )
so in order to pass the information to and from MATLAB, the FORTRAN90 function is required to have a header like this:
subroutine mexFunction ( nlhs, plhs, nrhs, prhs )
where nlhs counts the number of output arguments, and plhs is an integer array of pointers to those arguments, with nrhs and prhs being the analogous quantities for the input arguments.

The user's FORTRAN file must have an INCLUDE statement at the very beginning of the file of the following form:

      # include 
      
This statement is handled by the FORTRAN preprocessor, and so the FORTRAN file will probably need to have an extension of ".F90" (that is, CAPITAL F90) to indicate that it requires preprocessing. One of the things this include file does is to define a datatype called "mwpointer", which must be used to declare the arrays plhs and prhs.

The MATLAB Mex library includes a number of operators to extract information from the MATLAB input arguments, and to store the computed results into the output arguments.

Once the FORTRAN90 file is written, it must be "compiled" with the MATLAB MEX compiler, which will, as part of its work, also invoke some C compiler on your machine. The first time you use the MEX compiler, you may need to tell it where the appropriate compiler is, or which compiler to use. You can also, at any time, request that MEX switch to a different compiler, if there is a choice. To choose or change your compiler, type (inside of MATLAB)

mex -setup

Assuming your FORTRAN90 file is called fact.F90, for example, you can process it through the MEX compiler. You can always issue this command inside of MATLAB, and on some systems, the MEX compiler may be availabe directly as an external command. In either case, you issue the command:

mex fact.F90
which creates a compiled file whose name is system-dependent. On a Windows PC, it might be called fact.dll, and on a UNIX system, it might be something like fact.mexglx. In any case, the compiled file behaves essentially like a MATLAB M file called fact.m (except that it should, you hope, execute much faster than a MATLAB script).

In the very likely event that you have problems compiling the code, or using it from MATLAB, you may want to request the verbose compilation, with symbolic information added for debugging:

mex -v -g fact.F90

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Languages:

MATLAB_CALLS_F90 is available in a C version and a C++ version and a FORTRAN90 version.

Related Data and Programs:

C_CALLS_F90, C programs which illustrate a C program calling a FORTRAN90 subroutine.

C++_CALLS_F90, C++ programs which illustrate how a C++ main program can call a FORTRAN90 subroutine.

F90_CALLS_C, FORTRAN90 programs which illustrates how a FORTRAN90 program can call a C function.

F90_CALLS_C++, FORTRAN90 programs which illustrates how a FORTRAN90 program can call a C++ function.

F90_CALLS_MATLAB, FORTRAN90 programs which call MATLAB to carry out an auxillary calculation.

MATLAB_CALLS_C, MATLAB programs which call a C function using the MEX facility.

MEX, MATLAB programs which call lower-level functions written in traditional languages such as C, C++, FORTRAN77 or FORTRAN90, compiled with MATLAB's mex compiler.

Reference:

Examples:

FACT is an example in which a FORTRAN90 function is used to compute the factorial function. A single routine is used, which takes care of the "translation" between MATLAB and FORTRAN90, and the computation of the result.

CHEBY_U is an example in which a FORTRAN90 function is used to compute the Chebyshev U polynomial. The coding is done in such a way that the computation is in a separate FORTRAN90 function; the mexFunction is only used to "translate" between MATLAB and C.

You can go up one level to the MATLAB source codes.


Last revised on 30 September 2013.