How to create a structure and pass that structure's object as a pointer to a C function in Matlab/Simulink

5 visualizaciones (últimos 30 días)
Hello team,
We need to access a C-function in Matlab. This C function has a double pointer of structure type as an argument. Function updates the pointer with an address of structure field.
% % C code: Function in C
void C_function (structure1** ptr)
{
*ptr = &structure1_Obj.info
}
In order to access this function in Matlab (Using matlab function), we need to create the same structure in matlab to pass its object's pointer as an argument to the C function. So eventually, we will get the expected result available in Matlab. Hereby sharing the C code structure architectures for your reference:
% % C code: Structure(Nested) in C
typedef struct structure1
{
another_Structure info;
}structure1_Obj;
% % C code: Structure in C
typedef struct another_Structure
{
char name[50];
int age;
float height;
}
Please guide us to create the similar structure in Matlab in order to get the expected results in Matlab for further use.

Respuesta aceptada

Hitesh
Hitesh el 4 de Jun. de 2025
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:
  1. Use MEX interface (MATLAB Executable C/C++ code).
  2. Create equivalent structures in MATLAB using libstruct or struct with loadlibrary/calllib (for shared libraries).
  3. 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:
#include "mex.h"
#include <string.h>
// Define your structures
typedef struct {
char name[50];
int age;
float height;
} another_Structure;
typedef struct {
another_Structure info;
} structure1;
// Global object (example)
structure1 structure1_Obj = {
.info = {"John", 30, 5.9}
};
// The function to be wrapped
void C_function(structure1** ptr) {
*ptr = &structure1_Obj;
}
// The MEX gateway function
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
structure1* ptr = NULL;
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);
// Fill in fields
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.
mex C_function_mex.c
Make sure the C compiler is set up. If not, run:
mex -setup
Step 3: Call the function in MATLAB
result = C_function_mex();
For more information regarding "mex", kindly refer to the following MATLAB documentation
  2 comentarios
Shalaka
Shalaka el 4 de Jun. de 2025
Hello Hitesh,
Thank you for the quick response. I will try to implement the suggested approach.
I will follow up if necessary. Thanks!
Shalaka
Shalaka el 16 de Jun. de 2025
Hello Hitesh,
I have implemented the above approach. I can see the required structure fields in Matlab workspace after compilation and execution of the created Mex function. My next query is how to use this MEX file and its output inside a Simulink model?
I need to update certain variables in MATLAB workspace (in a Simulink model) based on this MEX output. Request you to please guide further for the same.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink Coder en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by