Using functions from C++ in MATLAB

조회 수: 7 (최근 30일)
Keegan Chafin
Keegan Chafin 2020년 6월 12일
답변: Anjaneyulu Bairi 2024년 11월 1일
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일
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에서 Call C from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by