Create MATLAB Data Array and Manage Memory from User-Managed Buffer - MATLAB & Simulink (original) (raw)

Main Content

This example C++ code shows how to use the MATLAB® Data API to create a MATLAB data array (MDA) from an array that has been allocated by a third-party library, without copying the data. It also shows how to control the lifetime of the MDA by using a custom deleter.

To create the MDA, write this function createMDA. The inputs are the dimensions of the MDA to create and a pointer to the memory allocated by librarylib. Assume that the functionget_raw_data_pointer obtains the underlying raw data pointer from an array allocated by the library.

To control the lifetime of the array, define a buffer with the data, provide a deleter (customDeleterFcn of type buffer_deleter_t), then pass the buffer to createArrayFromBuffer.

#include "MatlabDataArray.hpp" using namespace matlab::data; /*

}

For information about the buffer_ptr_t andbuffer_deleter_t data types, see MATLAB Data API Types.

For a MEX file example, see Using a custom deleter in a buffer_ptr_t in ArrayFacto​ry::create​ArrayFromB​uffer.

See Also

createArrayFromBuffer

Topics