Hi Shalaka,
I understand that you want to connect a C function with MATLAB where the function uses a double pointer of a structure type, you typically need to:
- Use MEX interface (MATLAB Executable C/C++ code).
- Create equivalent structures in MATLAB using libstruct or struct with loadlibrary/calllib (for shared libraries).
- Handle the pointer indirection carefully – because MATLAB doesn't directly support double ptr semantics in MATLAB space, you need to handle this pointer manipulation inside a MEX function.
Step 1: Convert your C code into a MEX-compatible C file.
Kindly refer to the following code for creating a MEX wrapper for the C function:
// Define your structures
// Global object (example)
structure1 structure1_Obj = {
.info = {"John", 30, 5.9}
// The function to be wrapped
void C_function(structure1** ptr) {
// The MEX gateway function
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
C_function(&ptr); // Get pointer to internal data
// Create MATLAB structure to return
const char* field_names[] = {"name", "age", "height"};
plhs[0] = mxCreateStructMatrix(1, 1, 3, field_names);
mxSetField(plhs[0], 0, "name", mxCreateString(ptr->info.name));
mxSetField(plhs[0], 0, "age", mxCreateDoubleScalar(ptr->info.age));
mxSetField(plhs[0], 0, "height", mxCreateDoubleScalar(ptr->info.height));
Step 2: Compile the MEX function in MATLAB using the following command.
Make sure the C compiler is set up. If not, run:
Step 3: Call the function in MATLAB
result = C_function_mex();
For more information regarding "mex", kindly refer to the following MATLAB documentation