Using functions from C++ in MATLAB

10 次查看(过去 30 天)
Keegan Chafin
Keegan Chafin 2020-6-12
回答: Anjaneyulu Bairi 2024-11-1,16:14
Hi all,
I'll start off by showing a function that I have in C++.
int __stdcall UDP_GigE_getData(unsigned char *packetType, T_GVSP_LEADER_PACKET *leader, T_GVSP_PAYLOAD_PACKET *payloads,
T_GVSP_TRAILER_PACKET *trailer, T_GVSP_ALL_IN_PACKET *allInOne)
You should know that all variables with type T_GVSP_... are struct types defined in C. Now, I want to use the calllib function in MATLAB to call the above function. Something like,
response = calllib( 'UDP_GigE' , 'UDP_GigE_getData' , packetType, leader, payloads, trailer, allInOne)
The problem is, I do not know how put those struct pointers in the calllib function, since MATLAB's pointer functionality is not to obvious to me. Does anyone have any ideas on how to put pointers into the calllib function correctly?
Thanks

回答(1 个)

Anjaneyulu Bairi
Anjaneyulu Bairi 2024-11-1,16:14
Hi,
To call a C++ function from MATLAB using "calllib", you need to ensure that the data types in MATLAB match those expected by the C++ function. In your case, you need to handle pointers to structures, which requires some setup in MATLAB.
  • Define the structures in MATLAB using "libstruct"
  • Create pointer to those structures using "libpointer"
  • Now call the function with created pointers like
calllib('UDP_GigE' , 'UDP_GigE_getData' , packetType, leaderptr, payloadsptr, trailerptr, allInOneptr)
You can refer the below documentation links to know more information on "libstruct","libpointer" and for few examples:

类别

Help CenterFile Exchange 中查找有关 C Shared Library Integration 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by