Call MATLAB Functions from Fortran MEX-Files

You can call MATLAB® functions, operators, user-defined functions, and other binary MEX-files from within your Fortran source code by using the API function mexCallMATLAB. The sincall.F example creates an mxArray, passes various pointers to a local function to acquire data, and calls mexCallMATLAB to calculate the sine function and plot the results. To see the source code, open the fileopen the file in MATLAB Editor. fill.F is the subroutine sincall calls to fill the mxArray with data. (Open the fileOpen the file in MATLAB Editor.)

It is possible to use mexCallMATLAB (or any other API routine) from within your computational Fortran subroutine. You can only call most MATLAB functions with double-precision data. Some functions that perform computations, such as eig, do not work correctly with data that is not double precision.

To build this example, at the command prompt type:

mex sincall.F fill.F

Running this example:

sincall

displays the results:

    Note:   You can generate an object of type mxUNKNOWN_CLASS using mexCallMATLAB. See the following example.

This function returns two variables but only assigns one of them a value:

function [a,b]=foo[c]
a=2*c;

If you then call foo using mexCallMATLAB, the unassigned output variable is now of type mxUNKNOWN_CLASS.

Was this topic helpful?