The MATLAB® interface automatically converts C++ function signatures into MATLAB function signatures. However, some C++ language constructs do not have unique
matches in the MATLAB language. To include this functionality in the interface, edit the
.mlx
definition file and replace the <DIRECTION>
,
<SHAPE>
, and <MLTYPE>
parameters with the
missing information. These parameters are used for the following cases.
To specify if a pointer argument is read-only input, output only, or a modifiable input argument, use the DIRECTION parameter.
If a pointer argument is used for array data, then dimension information is required to convert the array between C++ and MATLAB. Use the SHAPE parameter to specify this information.
C++ has many types representing string arguments. You might need to specify MLTYPE and
SHAPE values
so that MATLAB can correctly convert the C++ type to the MATLAB
string
type.
MATLAB offers code suggestions for values of these parameters. To activate suggestions for a specific parameter:
Uncomment the code defining the function.
Delete the parameter name, including the <>
characters.
Pause to allow the code suggestions to display.
If the suggestions do not appear, check that the
define
file is on your
MATLAB path.libName
.mlx
DIRECTION
ParameterIn C++, pointer arguments can be used to pass and return data from a function. Use the
DIRECTION
parameter to specify if the argument is read-only input,
output only, or a modifiable input argument.
The DIRECTION
parameter has one of these values:
input
—Input argument only
If a pointer argument is used to pass data to the function, then it must appear as an input argument in the MATLAB signature.
output
—Output argument only
If a pointer argument is used to retrieve data from the function, then it must appear as an output argument in the MATLAB signature.
inputoutput
—Input and output argument
If a pointer argument is used to both pass and return data, then it must appear as both an input argument and an output argument.
Note
Default arguments with direction specified as OUT
are not
supported. Define these with DIRECTION as INPUT
or
INPUTOUTPUT
in the MLX file.
For example, suppose that a C++ function passData
has the following
signature. The argument data
might be an input to the function, the
return value of the function, or input that the function modifies and returns. The
documentation of the function tells you how the function uses the argument
data
.
void passData(double *data);
Assuming data
is a scalar double value, this table shows the
MATLAB signature based on its role.
C++ Role for data | MATLAB Signature |
---|---|
Input data only to |
% Set DIRECTION = input
passData(data)
|
Return data only from |
% Set DIRECTION = output
[data] = passData()
|
Input and output data for |
% Set DIRECTION = inputoutput
[data] = passData(data)
|
SHAPE
ParameterIn C++, pointer arguments are used for both scalar data and array data. To use a pointer
as an array, dimension information is required to convert the array between C++ and
MATLAB. The SHAPE
parameter specifies the dimensions for the
pointer.
Note
These pointer types can only be used as scalars. Define SHAPE as 1
in the MLX file.
Pointers representing arrays of C++ class objects
Pointers to non-const primitive arrays returned from a function
The following examples of constructs defined in the sample
cppUseCases.hpp
header file show you how to specify the shape of an
argument. In these tables, the descriptions for the functions in the C++ Signature
and Role of Pointer column are based on assumed knowledge of the arguments. The
signature itself does not provide this information.
To view the cppUseCases.hpp
header file and its generated definition
file, see Sample C++ Library Definition File.
Define Pointer Argument to Fixed Scalar
C++ Signature and Role of Pointer | defineArgument Values |
---|---|
The input to function void readScalarPtr(int const * in) | For argument defineArgument(readScalarPtrDefinition, "in", ... "int32", "input", 1); |
The input to function void readScalarPtr(ns::MyClass2 const * in) | For argument defineArgument(readScalarPtrDefinition, "in", ... "clib.cppUseCases.ns.MyClass2", "input", 1); |
Define Pointer Argument
C++ Signature | defineArgument Values |
---|---|
The input to function void readMatrix1DPtr(int const * mat, size_t m) | For argument defineArgument(readMatrix1DPtrDefinition, "mat", ... "int32", "input", "m"); |
The input to function void readMatrix1DPtrFixedSize(int const * mat) | For argument defineArgument(readMatrix1DPtrFixedSizeDefinition, ... "mat", "int32", "input", 5); |
The input to function void readMatrix2DPtr(int const * mat, size_t m, size_t n) | For argument defineArgument(readMatrix2DPtrDefinition, "mat", ... "int32", "input", ["m","n"]); |
The input to function void readMatrix2DPtrFixedSize(int const * mat) | For argument defineArgument(readMatrix2DPtrFixedSizeDefinition, ... "mat", "int32", "input", 6); |
The input to function void readMatrix3DPtr(int const * mat, size_t m, size_t n, size_t p) | For argument defineArgument(readMatrix3DPtrDefinition, "mat", ... "int32", "input", ["m","n","p"]); |
Define Array Argument
C++ Signature | defineArgument Values |
---|---|
The input to function void readMatrix1DArr(int const [] mat, size_t len) | For argument defineArgument(readMatrix1DArrDefinition, "mat", ... "int32", "input", "len"); |
Define Output Pointer Argument
C++ Signature | defineArgument Values |
---|---|
The input to function int const * getRandomValues(size_t len) | For the return value defineOutput(getRandomValuesDefinition, "RetVal", ... "int32", "len"); |
The output argument of function
int const * getRandomValuesFixedSize() | For the return value defineOutput(getRandomValuesFixedSizeDefinition, ... "RetVal", "int32", 5); |
Define Scalar Object Argument
C++ Signature | defineArgument Values |
---|---|
The input to function double addClassByPtr(ns::MyClass2 const * myc2) | For the defineArgument(addClassByPtrDefinition, "myc2", ... "clib.cppUseCases.ns.MyClass2", "input", 1); |
The input to function void updateClassByPtr(ns::MyClass2 * myc2, double a, short b, long c) | For argument defineArgument(updateClassByPtrDefinition, "myc2", ... "clib.cppUseCases.ns.MyClass2", "input", 1); |
The input to function void readClassByPtr(ns::MyClass2 * myc2) | For argument defineArgument(readClassByPtrDefinition, "myc2", ... "clib.cppUseCases.ns.MyClass2", "input", 1); |
The input to function void fillClassByPtr(ns::MyClass2 * myc2, double a, short b, long c) | For argument defineArgument(fillClassByPtrDefinition, "myc2", ... "clib.cppUseCases.ns.MyClass2", "input", 1); |
Define Matrix Argument
C++ Signature | defineArgument Values |
---|---|
The input to function void updateMatrix1DPtrByX(int * mat, size_t len, int x) | For argument defineArgument(updateMatrix1DPtrByXDefinition, ... "mat", "int32", "inputoutput", "len"); |
The input to function void updateMatrix1DArrByX(int [] mat, size_t len, int x) | For argument defineArgument(updateMatrix1DArrByXDefinition, ... "mat", "int32", "inputoutput", "len"); |
The input to function int addValuesByPtr(int * mat, size_t len) | For argument defineArgument(addValuesByPtrDefinition, "mat", ... "int32", "input", "len"); |
The input to function int addValuesByArr(int [] mat, size_t len) | For argument defineArgument(addValuesByArrDefinition, ... "mat", "int32", "input", "len"); |
The function void fillRandomValuesToPtr(int * mat, size_t len) | For argument defineArgument(fillRandomValuesToPtrDefinition, ... "mat", "int32", "output", "len"); |
The function void fillRandomValuesToArr(int [] mat, size_t len) | For argument defineArgument(fillRandomValuesToArrDefinition, ... "mat", "int32", "output", "len"); |
Define String Argument
C++ Signature | defineArgument Values |
---|---|
The input to function char const * getStringCopy(char const * str) | For argument defineArgument(getStringCopyDefinition, "str", ... "string", "input", "nullTerminated"); |
The return value for function char const * getStringCopy(char const * str) | For return value defineOutput(getStringCopy, "RetVal", "char", 1); |
The input to function void readCharArray(char const * chArray, size_t len) | For argument defineArgument(readCharArrayDefinition, "chArray", ... "char", "input", "len"); |
The input to function void readInt8Array(char const * int8Array, size_t len) | For argument defineArgument(readInt8ArrayDefinition, "int8Array", ... "int8", "input", "len"); |
The return value for function char const * getRandomCharScalar() | For return value defineOutput(getRandomCharScalarDefinition, ... "RetVal", "char", 1); |
The type of the return value for function
char const * getRandomInt8Scalar() | For return value defineOutput(getRandomInt8ScalarDefinition, ... "RetVal", "int8", 1); |
The function void updateCharArray(char * chArray, size_t len) | For argument defineArgument(updateCharArrayDefinition, ... "chArray", "int8", "inputoutput", "len"); |
Define Typed Pointer Argument
C++ Signature | defineArgument Values |
---|---|
The input to function void useTypedefPtr(intDataPtr input1)
typedef int16_t intData; typedef intData * intDataPtr; | For argument defineArgument(useTypedefPtrDefinition, "input1", ... "int16", "input", 1); |
MLTYPE
ParameterMATLAB automatically converts C++ types to MATLAB types, as described in MATLAB to C++ Data Type Mapping. If the C++ type
for the argument is a string, then use these options to choose values for the
MLTYPE
and SHAPE
arguments.
C++ Type | MLTYPE | Options for SHAPE |
---|---|---|
char * |
| Scalar value |
const char * |
| Scalar value |
|
|