Effacer les filtres
Effacer les filtres

How to create a c++struct which is defined in extern c++ lib in MATLAB?

7 vues (au cours des 30 derniers jours)
yijia
yijia le 30 Avr 2024
Commenté : yijia le 30 Avr 2024
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:
  1. How to create char* argument in matlab?
  2. How to create a struct likes ASI_GPS_DATA * in matlab?
I tried libstruct function in matlab but failed, if anyone can help me?

Réponse acceptée

Harsh
Harsh le 30 Avr 2024
Modifié(e) : Harsh le 30 Avr 2024
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!
  1 commentaire
yijia
yijia le 30 Avr 2024
Hi Harsh, thank you very much!
Your answer is very specific! Folloing step by step perfectly solves my questions.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Call C++ from MATLAB dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by