Using the C Math Library | ![]() ![]() |
Optional Output Arguments
MATLAB functions may also have optional or multiple output arguments. For example, you invoke the find
function, which locates nonzero entries in arrays, with one, two, or three output arguments.
k = find(X); [i,j] = find(X); [i,j,v] = find(X);
In interpreted MATLAB, find
returns one, two, or three values. In C, a function cannot return more than one value. Therefore, the additional arrays must be passed to find
in the argument list. They are passed as pointers to mxArray
pointers (mxArray**
variables).
Output arguments always appear before input arguments in the parameter list. In order to accommodate all the combinations of output arguments, the MATLAB C Math Library mlfFind()
function takes three arguments, the first two of which are mxArray**
parameters corresponding to output values.
Using the MATLAB C Math Library, you call mlfFind
like this
mlfAssign(&k, mlfFind(NULL,NULL,X)); mlfAssign(&i, mlfFind(&j,NULL,X)); mlfAssign(&i, mlfFind(&j,&v,X));
where i
, j
, k
, v
, and X
are mxArray*
variables. i
, j
, k
, and v
are initialized to NULL
.
The general rule for multiple output arguments is that the function return value, an mxArray*
, corresponds to the first output argument. All additional output arguments are passed into the function as mxArray**
parameters.
![]() | Optional Input Arguments | Optional Input and Output Arguments | ![]() |