주요 콘텐츠

Define Missing Size Property

R2026b

When a C++ function uses pointer arguments to represent array data, MATLAB® needs explicit size information to convert that data safely. The Size property specifies the dimensions of pointer and array arguments so MATLAB can correctly interpret scalars, vectors, and multidimensional arrays when generating a C++ interface. In this context, Size is not inferred from the C++ signature itself. Instead, it is a property of the argument definition object—specifically, clibgen.api.InputArgumentDefinition or clibgen.api.OutputArgumentDefinition. You set the Size property to describe how MATLAB interprets the dimensions of the data associated with that argument.

For information about how MATLAB converts C/C++ data into equivalent MATLAB data types, see C++ to MATLAB Data Type Mapping.

Note

Pointers representing arrays of C++ class objects can only be used as scalars. Define Size as 1 in the clibgen.api.InterfaceDefinition object.

The following example C++ signatures 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 does not provide this information.

In these examples, function F is a FunctionDefinition object and argument A is an InputArgumentDefinition or OutputArgumentDefinition object of the function. Use the InputArgumentDefinition define or OutputArgumentDefinition define function to complete the argument definitions.

Note

Before R2026b: Use defineArgument functions in the library definition file to set <SHAPE>, <DIRECTION>, and <MLTYPE> values

Define Pointer Argument to Fixed Scalar

For fixed-width scalar integer and scalar object types, set the Size property to 1.

C++ Signature and Role of Pointer define Argument Values

The input to this function is a scalar pointer in.

void readScalarPtr(int const * in)

For argument in, call define with Size set to 1.

A = F.CPPInputs;
A.Size=1;

The input to this function is a scalar pointer to class ns::MyClass2.

void readScalarPtr(ns::MyClass2 const * in)

For argument in, call define with Size set to 1.

A = F.CPPInputs;
A.Size=1;

Define Pointer Argument

For pointer arguments, set Size to the length of each dimension.

C++ Signature define Argument Values

The input to this function is a pointer to an integer array of length m.

void readMatrix1DPtr(int const * mat, 
    size_t m)

For argument mat, set Size to argument m.

A = F.CPPInputs;
A(1).Size="m";

The input to this function is a pointer to a fixed-length array mat.

void readMatrix1DPtrFixedSize(int const * mat)

For argument mat, set Size to a fixed integer, such as 5.

A = F.CPPInputs;
A.Size=4;

The input to this function is a pointer to a two-dimensional integer matrix mat of size m-by-n.

void readMatrix2DPtr(int const * mat, 
    size_t m, size_t n)

For argument mat, set Size to ["m","n"].

A = F.CPPInputs;
A(1).Size= ["m","n"];

The input to this function is a pointer to a two-dimensional matrix mat of fixed dimensions.

void readMatrix2DPtrFixedSize(int const * mat)

For argument mat, set Size to a fixed integer, such as 6.

A = F.CPPInputs;
A.Size=6;

The input to this function is a pointer to a three-dimensional matrix mat of size m-by-n-by-p.

void readMatrix3DPtr(int const * mat, 
    size_t m, size_t n, size_t p)

For argument mat, set Size to ["m","n","p"].

A = F.CPPInputs;
A(1).Size= ["m","n","p"];

Define Array Argument

C++ Signature define Argument Values

The input to this function is a one-dimensional array mat of length len.

void readMatrix1DArr(int const [] mat, 
    size_t len)

For argument mat, set Size to length len.

A = F.CPPInputs;
A(1).Size= "len";

The input to this function is a two-dimensional array mat of size len by typeSz.

void readMatrix2DArr(int const [] mat, 
    size_t len, size_t typeSz)

For argument mat, set Size to ["len","typeSz"].

A = F.CPPInputs;
A(1).Size=["len","typeSz"];

Define Output Pointer Argument

C++ Signature define Argument Values

The input to this function is a pointer to an array of length len. The function returns a pointer argument as output.

int const * getRandomValues(size_t len)

For output argument RetVal, set Size to argument len.

A = F.CPPOutput;
A.Size= "len";

The input to this function is a pointer to an array of length len. The library provides a function named CustomDeleteFcn , which MATLAB can use to manage the memory of the argument.

int const * getRandomValues(size_t len)

For output argument RetVal, set Size to argument len. Specify CustomDeleteFcn as the DeleteFcn property.

A = F.CPPOutput;
A.define(Size="len",DeleteFcn="CustomDeleteFcn");

The output argument of this function is a pointer to a fixed-length array.

int const * getRandomValuesFixedSize()

For output argument RetVal, set Size to an integer, such as 5.

A = F.CPPOutput;
A.Size=5;

The output argument of this function is a pointer to a 2-by-2 array.

double * getRandomValuesArray()

For output argument RetVal, set Size to a [2, 2] array.

A = F.CPPOutput;
A.Size=[2,2];

The output argument of this function is a pointer to a 1-by-2 vector of type double. The library provides a function named CustomDeleteFcn, which MATLAB can use to manage the memory of the argument.

double * getRandomValuesDouble()

For output argument RetVal, set Size to 2. Specify CustomDeleteFcn as the DeleteFcn argument.

A = F.CPPOutput;
A.define(Size=2,DeleteFcn="CustomDeleteFcn")

The output argument of this function is a pointer to a 1-by-2 vector of type const double. The library provides a function named CustomDeleteFcn, which MATLAB can use to manage the memory of the argument.

double const * getRandomConstDouble()

For output argument RetVal, set Size to 2. Specify CustomDeleteFcn as the DeleteFcn argument.

A = F.CPPOutput;
A.define(Size=2,DeleteFcn="CustomDeleteFcn");

Define Additional Output Arguments

C++ Signature define Argument Values

This function returns an integer value and the value to which input argument arg points.

int getValues(int * arg)

For argument arg, change Direction to "output", change MATLABType to "int32", and set Size to 1.

A = F.CPPInputs;
A.define(Direction="output",Size=1,MATLABType="int32");

To call the function built into interface libname from MATLAB, type:

[out1, out2] = clib.libname.getValues

Define Scalar Object Argument

C++ Signature define Argument Values

The input to this function is a pointer to class ns::MyClass2.

double addClassByPtr(ns::MyClass2 const * myc2)

For the myc2 argument, set Size to 1.

A = F.CPPInputs;
A.Size=1;

The input to this function is a pointer to class ns::MyClass2.

void updateClassByPtr(ns::MyClass2 * myc2,
    double a, short b, long c)

For argument myc2, set Size to 1.

A = F.CPPInputs;
A(1).Size=1;

The input to this function is a pointer to class ns::MyClass2.

void readClassByPtr(ns::MyClass2 * myc2)

For argument myc2, set Size to 1.

A = F.CPPInputs;
A.Size=1;

The input to this function is a pointer to class ns::MyClass2.

void fillClassByPtr(ns::MyClass2 * myc2,
    double a, short b, long c)

For argument myc2, set Size to 1.

A = F.CPPInputs;
A(1).Size=1;

Define Matrix Argument

C++ Signature define Argument Values

The input to this function is a pointer to an integer vector of length len. The argument x modifies the input argument.

void updateMatrix1DPtrByX(int * mat, 
    size_t len, int x)

For argument mat, set Direction to "inputoutput" and Size to "len".

A = F.CPPInputs;
A(1).define(Direction="inputoutput",Size="len;

The input to this function is a reference to an integer array of length len. Argument x modifies input argument mat.

void updateMatrix1DPtrByX(int [] mat,  
    size_t len, int x)

For argument mat, set Direction to "inputoutput" and Size to "len".

A = F.CPPInputs;
A(1).Size="len";

The input to this function is a pointer to an integer vector of length len. The function does not modify the input argument.

int addValuesByPtr(int * mat,
    size_t len)

For argument mat, set Direction to "input" and Size to "len".

A = F.CPPInputs;
A(1).Size="len";

The input to this function is a reference to an integer array of length len. The function does not modify the input argument.

int addValuesByArr(int [] mat, 
    size_t len)

For argument mat, set Direction to "input" and Size to "len".

A = F.CPPInputs;
A(1).Size="len";

Define String Argument

C++ Signature define Argument Values

The input to this function is a C-style string.

char const * getStringCopy(char const * str)

For argument str, set MATLABType to "string" and Size to "nullTerminated".

A = F.CPPInputs;
A(1).define(MATLABType="string", ...
  Size="nullTerminated");

The return value for this function is a string.

char const * getStringCopy(char const * str)

For output argument RetVal, set MATLABType to "string" and Size to "nullTerminated".

A = F.CPPOutput;
A(1).define(MATLABType="string",Size="nullTerminated");

The return value for this function is a string of length buf.

void getMessage(char * pmsg, int buf)

MATLAB defines argument pmsg as an input variable of type clib.array.libname.Char. To define pmsg as an output variable of type string:

  • Replace "input" with "output".

  • Add the "NumElementsInBuffer" name-value argument set to variable buf.

A = F.CPPOutput;
A(1).define(MATLABType="string", ...
  Size="nullTerminated",NumElementsInBuffer="buf");

The input to this function is a string specified by length len.

void readCharArray(char const * chArray,
    size_t len)

For argument chArray, set MATLABType to "char" and Size to "len".

A = F.CPPInputs;
A(1).define(MATLABType="char",Size="len");

The input to this function is an array of type int8 and length len.

void readInt8Array(char const * int8Array,
    size_t len)

For argument int8Array, set MATLABType to "int8" and Size to "len".

A = F.CPPInputs;
A(1).define(MATLABType="int8",Size="len");

The return value for this function is a scalar of characters.

char const * getRandomCharScalar()

For output argument RetVal, set MATLABType to "char" and Size to 1.

A = F.CPPOutput;
A.define(MATLABType="char",Size=1);

The return value for this function is a 2 element character vector.

char * getRandomChars()

For output argument RetVal, set MATLABType to "char" and Size to 2.

The return value for this function is a 2 element character vector. The library provides a function named CustomDeleteFcn, which MATLAB can use to manage the memory of the argument.

char * getRandomChars()

For output argument RetVal, set MATLABType to "char" and Size to 2. Specify CustomDeleteFcn as the DeleteFcn argument.

A = F.CPPOutput;
A.define(MATLABType="char",Size=2, ...
  DeleteFcn="CustomDeleteFcn");

The type of the return value for this function is int8.

char const * getRandomInt8Scalar()

For output argument RetVal, set MATLABType to "int8" and Size to 1.

A = F.CPPOutput;
A.define(MATLABType="int8",Size=1);

This function updates the input argument chArray. The length of chArray is len.

void updateCharArray(char* chArray,
    size_t len)

For argument chArray, set Direction to "inputoutput" and Size to "len".

The input to these functions is an array of C-string of size numStrs.

void readCStrArray(char** strs, int numStrs);
void readCStrArray(char* strs[], int numStrs);

For argument strs, set Size to the array ["numStrs", "nullTerminated"].

The input to these functions is a const array of C-string of size numStrs.

void readConstCStrArray (const char** strs, int numStrs);
void readConstCStrArray (const char* strs[], int numStrs);

Set the InterfaceConfiguration property TreatConstCharPointerAsCString to true to define Size for argument strs as ["numStrs", "nullTerminated"].

The input to this function is a fixed-size array of C-string.

void readFixedCStrArray (char* strs[5]);

For argument strs, set Size to the array [5, "nullTerminated"].

The input to this function is a fixed-size const array of C-string.

void readConstCFixedStrArray (const char* strs[5]);

Set the InterfaceConfiguration property TreatConstCharPointerAsCString to true to define Size for argument strs as [5, "nullTerminated"].

Define Typed Pointer Argument

C++ Signature define Argument Values

The input to this function is a pointer to typedef intDataPtr.

void useTypedefPtr(intDataPtr input1)

intDataPtr is defined as:

typedef int16_t intData;
typedef intData * intDataPtr;

For argument input1, set Direction to input and Size to 1.

A = F.CPPInputs;
A.define(Direction="input",Size=1);

Use Property or Method as Size

You can use a public nonstatic C++ data member (property) as the Size property for the return type of a nonstatic method or another nonstatic data member (property) in the same class. The property must be defined as an integer (C++ type int). Similarly, you can use static C++ data members as a Size property for a return type of a static method or another static data member in the same class.

You can use a public, nonstatic C++ method as the Size property for a nonstatic property or for the return type of a nonstatic method in the same class. The method must be fully implemented, without input arguments, and the return type must be defined as a C++ type int.

You can use a combination of arguments, properties, and methods as the Size property for a method return type. If the specified Size exists as both a parameter and a method or property, then parameters take precedence. In this case Size is treated as a parameter.

C++ Signature Size Values

The size of data member rowData is defined by data members rows and cols and by the result of method channels.

class A
{
public:
    int rows;
    int cols;
    int* rowData;
    int channels();
};

For property rowData, set Size to an array of rows, cols, and channels.

The size of the array returned by getData is defined by data members rows and cols and by the result of the channels method.

class B
{
public:
    int rows;
    int cols;
    int* rowData;
    int channels();
    const int* getData();
};

For the return value of method getData, set Size to an array of rows, cols, and channels.

The size of the array returned by getData is defined by the rows parameter and the result of method channels.

class C
{
public:
    int rows;
    int channels();
    const int* getData (int rows);
};

For the return value of method getData, set Size to an array of parameter rows and method channels.

See Also

Objects

Topics