This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Fortran Source MEX Files

Write Fortran subroutines using MATLAB® API libraries

A MEX file lets you call a Fortran subroutine from MATLAB. To create a MEX file, you need:

For information about writing C/C++ MEX files, see C Source MEX Files.

For information about using the loadlibrary and calllib commands to call functions in shared libraries, see Call C Shared Libraries.

For information about writing S-functions using Fortran code, see your Simulink® documentation.

MEX files are not appropriate for all applications. MATLAB is a high-productivity environment whose specialty is eliminating time-consuming, low-level programming in compiled languages like Fortran. In general, do your programming in MATLAB. Do not use MEX files unless your application requires it.


Create Fortran Source MEX File

This example shows how to write a MEX file to call a Fortran subroutine, timestwo, in MATLAB using a MATLAB matrix.

Table of MEX File Source Code Files

This table links to source code for MEX examples.

User Messages

To print text in the MATLAB Command Window, use the mexPrintf function.

Error Handling

The mexErrMsgIdAndTxt function prints error information and terminates your binary MEX file.

Components of Fortran MEX File

The gateway routine is the entry point to the MEX file.

MATLAB Fortran API Libraries

The Matrix Library and the MEX Library describe functions you can use in your gateway and computational routines that interact with MATLAB programs and the data in the MATLAB workspace.

Data Flow in Fortran MEX Files

Suppose your MEX-file myFunction has two input arguments and one output argument.

Memory Management

When a MEX file returns control to MATLAB, it returns the results of its computations in the output arguments—the mxArrays contained in the left-side arguments plhs[].

Handling Large mxArrays

Binary MEX-files built on 64-bit platforms can handle 64-bit mxArrays.

Was this topic helpful?