Using the C Math Library | ![]() ![]() |
How to Call MATLAB Functions
Some MATLAB functions accept optional input arguments and return multiple output values. Some MATLAB functions can take a varying number of input and output values; these functions are called varargin
and varargout
functions.
C does not allow routine with the same name to accept different calling sequences nor does it allow a routine to return more than one value.
Thus, to translate MATLAB functions into callable C routines, the MATLAB C Math Library had to establish certain calling conventions. This chapter describes these conventions.
One Output Argument and Only Required Input Arguments
For MATLAB functions that do not have optional input arguments and that return only a single value, the translation to C syntax is direct. For example, in interpreted MATLAB, you invoke the cosine function, cos
, like this
Y = cos(X);
where both X
and Y
are arrays.
Using the MATLAB C Math Library, you invoke cosine in much the same way
mlfAssign(&Y, mlfCos(X));
where both X
and Y
are pointers to mxArray
structures. Y
must be initialized to NULL
. mlfAssign()
assigns the return value from mlfCos()
to Y
.
Note
The example above and all the remaining examples in this chapter use the automated memory management routine mlfAssign() to bind the array return value to a variable. For information about writing functions that use mlfAssign() and C Math Library automated memory management, see Chapter 4, Managing Array Memory..
|
![]() | Calling Library Routines | Optional Input Arguments | ![]() |