A MEX file lets you call a Fortran subroutine from MATLAB. To create a MEX file, you need:
The ability to write Fortran source code. You can create these files with the MATLAB Editor.
A compiler supported by MATLAB. For an up-to-date list of supported compilers, see the Supported and Compatible Compilers website.
mex build script. For more
information, see Executable Fortran MEX Files.
For information about writing C/C++ MEX files, see C/C++ Source Files.
For information about using the
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.
This example shows how to write a MEX file
to call a Fortran subroutine,
timestwo, in MATLAB using
a MATLAB matrix.
This table links to source code for MEX examples.
To print text in the MATLAB Command Window, use the
mexErrMsgIdAndTxt function prints error information and terminates your binary MEX file.
The gateway routine is the entry point to the MEX file.
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.
Suppose your MEX-file
myFunction has two input arguments and one output argument.
a MEX file returns control to MATLAB, it returns the results
of its computations in the output arguments—the
contained in the left-side arguments
Binary MEX-files built on 64-bit platforms can handle 64-bit