MATLAB Compiler | ![]() ![]() |
C Interface Functions
The C interface functions process any input arguments and pass them to the implementation version of the function, M
f
.
mlxF Interface Function
The Compiler always generates the mlx
F
interface function, which is used by feval
. At times, the Compiler needs to use feval
to perform argument matching even if the user does not specifically call feval
. For example,
would use the feval
interface. The following C code is the corresponding feval
interface (mlxGasket
) from the Sierpinski Gasket example. This function calls the C Mgasket
function.
Note Comments have been added to the generated code to highlight where the input and output arguments are processed and where functions are called. |
/* * The function "mlxGasket" contains the feval interface * for the "gasket" M-function from file * "<matlab>\extern\examples\compiler\gasket.m" (lines 1-23). * The feval function calls the implementation version of * gasket through this function. This function processes * any input arguments and passes them to the * implementation version of the function, appearing above. */ void mlxGasket(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]) { mxArray * mprhs[1]; mxArray * mplhs[1]; int i; /* ------------- Input Argument Processing ------------ */ if (nlhs > 1) { mlfError( mxCreateString( "Run-time Error: File: gasket Line: 1 Column: " "1 The function \"gasket\" was called with mor" "e than the declared number of outputs (1)."), NULL); } if (nrhs > 1) { mlfError( mxCreateString( "Run-time Error: File: gasket Line: 1 Column: " "1 The function \"gasket\" was called with mor" "e than the declared number of inputs (1)."), NULL); } for (i = 0; i < 1; ++i) { mplhs[i] = NULL; } for (i = 0; i < 1 && i < nrhs; ++i) { mprhs[i] = prhs[i]; } for (; i < 1; ++i) { mprhs[i] = NULL; } /* ---------------------------------------------------- */ mlfEnterNewContext(0, 1, mprhs[0]); /* -------- Call to C Implementation Function --------- */ mplhs[0] = Mgasket(nlhs, mprhs[0]); /* ------------- Output Argument Processing ----------- */ mlfRestorePreviousContext(0, 1, mprhs[0]); plhs[0] = mplhs[0]; }
mlfF Interface Function
The Compiler always generates the mlf
F
interface function, which contains the "normal" C interface to the function. This code is the corresponding C interface function (mlfGasket
) from the Sierpinski Gasket example. This function calls the C mgasket
function:
/* * The function "mlfGasket" contains the normal interface * for the "gasket" M-function from file * "<matlab>\extern\examples\compiler\gasket.m" (lines 1-23). * This function processes any input arguments and passes * them to the implementation version of the function, * appearing above. */ mxArray * mlfGasket(mxArray * numPoints) { int nargout = 1; /* ------------- Input Argument Processing ------------ */ mxArray * theImage = NULL; mlfEnterNewContext(0, 1, numPoints); /* ----------------- Call M-Function ------------------ */ theImage = Mgasket(nargout, numPoints); /* ------------- Output Argument Processing ----------- */ mlfRestorePreviousContext(0, 1, numPoints); return mlfReturnValue(theImage); }
mlfNF Interface Function
The Compiler produces this interface function only when the M-function uses the variable nargout
.The nargout
interface allows you to specify the number of requested outputs via the int nargout
argument, as opposed to the normal interface that dynamically calculates the number of outputs based on the number of non-NULL inputs it receives.
This is the corresponding mlfN
F
interface function (mlfNFoo
) for the foo.m example described earlier in this chapter. This function calls the Mfoo
function that appears in foo.c
:
/* * The function "mlfNFoo" contains the nargout interface * for the "foo" M-function from file * "<matlab>\extern\examples\compiler\foo.m" (lines 1-8). * This interface is only produced if the M-function uses * the special variable "nargout". The nargout interface * allows the number of requested outputs to be specified * via the nargout argument, as opposed to the normal * interface, which dynamically calculates the number of * outputs based on the number of non-NULL inputs it * receives. This function processes any input arguments * and passes them to the implementation version of the * function, appearing above. */ mxArray * mlfNFoo(int nargout, mxArray * * b, mxArray * x, mxArray * y) { /* ------------- Input Argument Processing ------------ */ mxArray * a = NULL; mxArray * b__ = NULL; mlfEnterNewContext(1, 2, b, x, y); /* ----------------- Call M-Function ------------------ */ a = Mfoo(&b__, nargout, x, y); /* ------------- Output Argument Processing ----------- */ mlfRestorePreviousContext(1, 2, b, x, y); if (b != NULL) { mclCopyOutputArg(b, b__); } else { mxDestroyArray(b__); } return mlfReturnValue(a); }
mlfVF Interface Function
The Compiler produces this interface function only when the M-function uses the variable nargout
and has at least one output. This void
interface function specifies zero output arguments to the implementation version of the function, and in the event that the implementation version still returns an output (which, in MATLAB, would be assigned to the ans
variable), it deallocates the output.
This is the corresponding mlfV
F
interface function (mlfVFoo
) for the foo.m example described at the beginning of this section. This function calls the C Mfoo
implementation function that appears in foo.c
:
/* * The function "mlfVFoo" contains the void interface for * the "foo" M-function from file * "<matlab>\extern\examples\compiler\foo.m" (lines 1-8). The * void interface is only produced if the M-function uses * the special variable "nargout", and has at least one * output. The void interface function specifies zero * output arguments to the implementation version of the * function, and in the event that the implementation * version still returns an output (which, in MATLAB, would * be assigned to the "ans" variable), it deallocates the * output. This function processes any input arguments and * passes them to the implementation version of the * function, appearing above. */ void mlfVFoo(mxArray * x, mxArray * y) { /* ------------- Input Argument Processing ------------ */ mxArray * a = NULL; mxArray * b = NULL; mlfEnterNewContext(0, 2, x, y); /* ----------------- Call M-Function ------------------ */ a = Mfoo(&b, 0, x, y); /* ------------- Output Argument Processing ----------- */ mlfRestorePreviousContext(0, 2, x, y); mxDestroyArray(a); mxDestroyArray(b); }
![]() | Internal Interface Functions | C++ Interface Functions | ![]() |