Using the C++ Math Library | ![]() ![]() |
Representing Input Arguments As a Cell Array
In MATLAB you can substitute a cell array for a comma-separated list of MATLAB variables when you pass input arguments to a function. MATLAB treats the contents of each cell as a separate input argument. To trigger this functionality, you specify multiple values by indexing into the cell array with, for example, the colon index or a vector index.
For example, the MATLAB expression
T{1:5}
when passed as an input argument is equivalent to a comma-separated list of the contents of the first five cells of T
. Simply passing the cell array T
produces an error.
The MATLAB C++ Math Library also supports the expansion of the contents of a cell array into separate input arguments for library functions. For functions that implement MATLAB varargin
functions, you use mwArray::cell
to obtain an array reference that returns multiple values.
For example, given the varargin
function
void varargin_func(mwArray a, mwArray b, const mwVarargin &varargin const mwArray v1=mwArray::DIN, ..., . . . const mwArray &v32=mwArray::DIN);
you can make the following call:
varargin_func(A, B, C.cell(colon(1,5)));
A
and B
, existing mwArray
s, are passed as explicit arguments. C
is a cell array that contains at least five cells. The embedded call to cell()
uses the index {1:5}
to return multiple values: the first five cells of C. The MATLAB C++ Math Library passes these as individual arguments to varargin_func()
.
Location of the Indexed Cell Array in the Argument List
For example, you cannot make this call to the example varargin
function.
varargin_func(C.cell(1,5), A, B);
Given the definition of varargin_func()
, the first argument position is reserved for an explicit, single argument. The MATLAB C++ Math Library does not handle multiple values in an explicit position.
varargin
argument position.See Indexing into Cell Arrays in Chapter 4 to learn more about indexing into cell arrays.
![]() | Example Program: Passing Functions As Arguments (ex3.cpp) | Using the Mathematical Operators | ![]() |