How to convert a C structure (returned by dll) to MATLAB structure?

4 次查看(过去 30 天)
Hi,
I have a dll that can read A2L files to a C structure that I would then link to manipulate in MATLAB so that I can use information from Simulink to make changes.
Is there a way for MATLAB to read the C structure as a MATLAB structure?
I have a header file that describes the structure and functions.
Thanks
  1 个评论
Rik
Rik 2021-12-10
You could have a look on this GitHub page, which contains C code to deal with Matlab structs. I don't know what the 'correct' way would be to deal with the conversion.

请先登录,再进行评论。

回答(1 个)

Sameer
Sameer 2024-2-19
编辑:Sameer 2024-2-19
Hi Jack,
I understand that you're looking to integrate a C structure from a DLL into MATLAB and interact with it as a MATLAB structure. To achieve this, you can write custom code using a MATLAB Executable (MEX) file. MEX files serve as a bridge, allowing you to execute C, C++, or Fortran code within MATLAB's environment.
Let's consider a scenario where you have a C structure that represents a point in 3D space, and you have a DLL that provides this structure. The structure in C might look like this:
// C structure for a 3D point
typedef struct
{
double x;
double y;
double z;
} Point3D;
Assuming you have a function in your DLL that returns this Point3D structure, and you want to convert it into a MATLAB structure. Here's how you can write a MEX function to do this:
Step 1: Include Necessary Headers
#include "mex.h"
// Include the header that defines Point3D and the function to get a point
#include "your_header.h"
Step 2: Define the MEX Function The entry point of a MEX file is the mexFunction. It has a specific signature that MATLAB recognizes:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// code
}
Step 3: Get the Point3D Structure from the DLL Inside mexFunction, call the DLL function that returns the Point3D structure:
// Assume getPointFromDLL is a function in your DLL that returns a Point3D
Point3D point = getPointFromDLL();
Step 4: Create MATLAB Structure Define the field names and create the MATLAB structure that corresponds to the Point3D C structure:
// Define field names
const char *fieldNames[] = {"x", "y", "z"};
int numberOfFields = 3; // We have three fields: x, y, z
// Create a 1x1 MATLAB structure with these fields
plhs[0] = mxCreateStructMatrix(1, 1, numberOfFields, fieldNames);
Step 5: Populate the MATLAB Structure Convert the C structure fields into MATLAB data and populate the MATLAB structure:
// Populate the MATLAB structure with data from the Point3D structure
mxSetField(plhs[0], 0, "x", mxCreateDoubleScalar(point.x));
mxSetField(plhs[0], 0, "y", mxCreateDoubleScalar(point.y));
mxSetField(plhs[0], 0, "z", mxCreateDoubleScalar(point.z));
Put it all together to form the complete MEX function:
#include "mex.h"
#include "your_header.h"
// Example C structure for a 3D point
typedef struct
{
double x;
double y;
double z;
} Point3D;
// Function to interface with the DLL (placeholder)
Point3D getPointFromDLL()
{
Point3D point;
// Assume your DLL populates this structure
point.x = 1.0;
point.y = 2.0;
point.z = 3.0;
return point;
}
// MEX function to convert Point3D to MATLAB structure
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Get the Point3D structure from the DLL
Point3D point = getPointFromDLL();
// Define field names
const char *fieldNames[] = {"x", "y", "z"};
int numberOfFields = 3;
// Create a 1x1 MATLAB structure with these fields
plhs[0] = mxCreateStructMatrix(1, 1, numberOfFields, fieldNames);
// Populate the MATLAB structure with data from the Point3D structure
mxSetField(plhs[0], 0, "x", mxCreateDoubleScalar(point.x));
mxSetField(plhs[0], 0, "y", mxCreateDoubleScalar(point.y));
mxSetField(plhs[0], 0, "z", mxCreateDoubleScalar(point.z));
}
Step 6: Compile the MEX File In MATLAB:
mex your_mex_file.c
Step 7: Once compiled, you can call the MEX file from MATLAB like any other function:
pointStruct = your_mex_file;
%This will return a MATLAB structure pointStruct with fields x, y, and z.
For additional information, please refer to the links below:

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by