How to create a c++struct which is defined in extern c++ lib in MATLAB?
4 次查看(过去 30 天)
显示 更早的评论
I want to call a c++ method in matlab like:
loadlibrary('ASICamera2');
p = libpointer('string');
gpsData = struct();
x = calllib('ASICamera2','ASIGetDataAfterExpGPS', 0, p, 256*256, gpsData);
In 'ASICamera2.h',method 'ASIGetDataAfterExpGPS' is defined as follow. Obviously, the above matlab code dosen't work because p and gpsData are not correct data type.
int ASIGetDataAfterExpGPS(int iCameraID, unsigned char* pBuffer, long lBuffSize, ASI_GPS_DATA *gpsData);
typedef struct _ASI_GPS_DATA {
ASI_DATE_TIME Datetime;
double Latitude;
double Longitude;
char Unused[64];
} ASI_GPS_DATA;
typedef struct _ASI_DATE_TIME{
int Year;
char Unused[64];
} ASI_DATE_TIME;
Now I have two questions:
- How to create char* argument in matlab?
- How to create a struct likes ASI_GPS_DATA * in matlab?
I tried libstruct function in matlab but failed, if anyone can help me?
0 个评论
采纳的回答
Harsh
2024-4-30
编辑:Harsh
2024-4-30
Hi,
From what I can gather, you are trying to use a C/C++ function in MATLAB which uses a struct and are facing issues while using the structure.
Follow the following steps to initialize a C/C++ structure and char* in MATLAB:
(sample.c)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct {
int id;
char* value;
} sampleStruct;
sampleStruct* initStruct(int id, char* value) {
sampleStruct* s = (sampleStruct*)malloc(sizeof(sampleStruct));
s->id = id;
s->value = strdup(value);
return s;
}
sampleStruct* useStruct(sampleStruct* s) {
return s;
}
Compile *.so file of sample.c
gcc -shared -fpic -o sample.so sample.c
Define a sample.h file
typedef struct {
int id;
char* value;
} sampleStruct;
sampleStruct* initStruct(int id, char* value);
sampleStruct* useStruct(sampleStruct* s);
Using the loadlibrary function, load sample.h
loadlibrary('sample.so', 'sample.h')
Call useStruct function which uses a structure from MATLAB using the calllib function in MATLAB.
s_ptr = calllib('sample', 'initStruct', 123, 'sampleStr');
val = calllib('sample', 'useStruct', s_ptr);
disp(val)
I hope this help, thanks!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!