Suppose that your MEX file
myFunction has two input
arguments and one output argument. The MATLAB® syntax is
[X] = myFunction(Y, Z). To
myFunction from MATLAB, type:
X = myFunction(Y, Z);
The MATLAB interpreter calls
gateway routine to
myFunction, with these
Your input is
prhs, a two-element array
nrhs = 2). The first element is a pointer to
Y and the
second element is a pointer to an
Your output is
plhs, a one-element array
nlhs = 1) where the single element is a
null pointer. The parameter
plhs points at nothing because the output
X is not created until the subroutine
The gateway routine creates the output array and sets a pointer to it
plhs. If the routine does not assign a value
plhs but you assign an output value to the
function when you call it, MATLAB generates an error.
It is possible to return an output value even if
0, which corresponds to returning the result in
This MEX Cycle diagram shows how inputs enter a MEX file, what functions the gateway routine performs, and how outputs return to MATLAB.
In this example, the syntax of the MEX file
[C, D] = func(A,B). In the figure, a call to
func tells MATLAB to pass variables
B to your MEX file.
D are left unassigned.
The gateway routine uses the
mxCreate* functions to create the MATLAB arrays for your output arguments. It sets
plhs to the
pointers to the newly created MATLAB arrays. It uses the
to extract your data from your input arguments
Finally, it calls your computational routine, passing the input and
output data pointers as function parameters.
Fortran MEX Cycle