call C++ from Matalb

2 visualizzazioni (ultimi 30 giorni)
Harris
Harris il 14 Giu 2017
Risposto: Vivek il 2 Ott 2019
Dear technical support
I want to call a function written in c++ from Matlab, but do not have an idea how to do it.
I wrote a c++ function which is dependent on an open c++ library (Point cloud library (PCL)), and now I need to use this function in Matlab.
I read the example here: https://au.mathworks.com/help/matlab/matlab_external/standalone-example.html
This example looks good, but it does not show how to link another c++ library like PCL, which is what I need.
Another example shows how to call a c function. https://au.mathworks.com/help/matlab/ref/loadlibrary.html
But I don’t know how to make it work for me. Do I need firstly convert my c++ function to a static library and then use the loadlibrary function?
Thank you for your help!
Regards!
Huajian liu

Risposte (4)

Vivek
Vivek il 2 Ott 2019
We've introduced this capability in MATLAB starting in R2019a. Please take a look:

Walter Roberson
Walter Roberson il 15 Giu 2017
You do not need to use a static library. However, you need to build a C-compatible interface to the library. It is not possible for MATLAB to call into a C++ object member function: it can only call into C++ static functions and static class functions that defined with
extern "C"
It is, however, possible for a call to a static class function to return an opaque pointer to a C++ object as long as it converts it using (void *) and the MATLAB routine does not do any calculation on it.

Harris
Harris il 15 Giu 2017
Thank Walter.
My c++ function is not a object member function, and it is a static function. But this function used some other member functions in Point Cloud library. How to build a C-compatible interface? Can you provide an example?
Regards!
Harris

HANS
HANS il 19 Mar 2018
Hi to all :)
I have name.mex function and many .cpp and .h files related to it. name.mex takes only 1xinf vector which is a complex double values inside and output is the same size vector.
I want to call this name.mex within C++. As I know I have to use "int mexCallMATLAB(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[], const char *command_name);"
Please could anybody give a usage example in C++ ? I guess I have to use "ifstream" to load the data vector before I use the function but do I have to define via "mxArray" or ??
Thx, WR

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by