User`s manual
Fortran MEX-Files
3-7
plhs is a 1-element C array where the single element is a null pointer. prhs is
a 2-element C array where the first element is a pointer to an
mxArray named
Y and the second element is a pointer to an mxArray named Z.
The parameter
plhs points at nothing because the output x is not created until
the subroutine executes. It is the responsibility of the gateway routine to create
an output array and to set a pointer to that array in
plhs(1). If plhs(1) is left
unassigned, MATLAB prints a warning message stating that no output has
been assigned.
Note It is possible to return an output value even if nlhs = 0. This
corresponds to returning the result in the ans variable.
The gateway routine should validate the input arguments and call
mexErrMsgTxt if anything is amiss. This step includes checking the number,
type, and size of the input arrays as well as examining the number of output
arrays. The examples included later in this section illustrate this technique.
The
mx functions provide a set of access methods (subroutines) for
manipulating MATLAB arrays. These functions are fully documented in the
online API reference pages. The
mx prefix is shorthand for mxArray and it
means that the function enables you to access and/or manipulate some of the
information in the MATLAB array. For example,
mxGetPr gets the real data
from the MATLAB array. Additional routines are provided for transferring
data between MATLAB arrays and Fortran arrays.
The gateway routine must call
mxCreateFull, mxCreateSparse, or
mxCreateString to create MATLAB arrays of the required sizes in which to
return the results. The return values from these calls should be assigned to the
appropriate elements of
plhs.
The gateway routine may call
mxCalloc to allocate temporary work arrays for
the computational routine if it needs them.
The gateway routine should call the computational routine to perform the
desired calculations or operations. There are a number of additional routines
that MEX-files can use. These routines are distinguished by the initial
characters
mex, as in mexCallMATLAB and mexErrMsgTxt.