I'm interfacing Matlab and C-code in order to be able to directly use some C-functions in Matlab. I know the prototype of these functions but the code inside could change. In order to interface it all, I use loadlibrary and calllib in Matlab and I don't want to use MEXFiles.
In the C-code, some structures are defined. Nevertheless, the one which defines the base component of the code could change : it contains some structure and variables, but some other structures can be added in this one, and I would like Matlab to be able to deal with all that.
But, as Mathworks says it :
Nested structures or structures containing a pointer to a structure are not supported. However, MATLAB can access an array of structures created in an external library.
So I can't store directly a nested structure in Matlab. For example, the main component is a structure (a). This one contains an other structure (b). And (b) contains pointers to functions. If I directly store (a) in a variable using libstruct, when I call a C-method with (a) in argument, we can see that the pointers are lost. The worst is that the C-code knows what is the pointer to the structure (b), but he can't access to the pointed funcitons. Nevertheless, by creating this structure (b) before in Matlab, it works, but it's specific to (b) and I can't do the same for other nested structures.
That's why I was thinking that I must prevent Matlab from looking the type of the variable. I just must give it the pointer to the structure and lock all is in relation with this pointer in order to be able to pass this pointer in argument of a C-function with calllib.
So that's my question : do you know if I can lock a part of the memory which contains the structure (a) and all of what is in relation with? And do you think I can prevent Matlab from looking what is this pointer?
In fact, I just want to create a nested structure in a C-function and to re-use it in an other C-function, but to call these two C-functions with Matlab (without using MexFiles).
Thanks! :)
C-code
Structure (a)
typedef struct {
fmi2Real *r;
fmi2Integer *i;
fmi2Boolean *b;
fmi2String *s;
fmi2Boolean *isPositive;
fmi2Real time;
fmi2String instanceName;
fmi2Type type;
fmi2String GUID;
const fmi2CallbackFunctions *functions;
fmi2Boolean loggingOn;
fmi2Boolean logCategories[NUMBER_OF_CATEGORIES];
fmi2ComponentEnvironment componentEnvironment;
ModelState state;
fmi2EventInfo eventInfo;
int isDirtyValues; // !0 is true
} ModelInstance;
Other structure can be added in this structure (a).
Structure (b)
typedef struct {
const fmi2CallbackLogger logger;
const fmi2CallbackAllocateMemory allocateMemory;
const fmi2CallbackFreeMemory freeMemory;
const fmi2StepFinished stepFinished;
const fmi2ComponentEnvironment componentEnvironment;
} fmi2CallbackFunctions;
typedef void (*fmi2CallbackLogger) (fmi2ComponentEnvironment, fmi2String, fmi2Status, fmi2String, fmi2String, ...);
typedef void* (*fmi2CallbackAllocateMemory)(size_t, size_t);
typedef void (*fmi2CallbackFreeMemory) (void*);
typedef void (*fmi2StepFinished) (fmi2ComponentEnvironment, fmi2Status);
Prototype of one of the C-function (the first one which creates the main component)
fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2String fmuGUID,
fmi2String fmuResourceLocation, const fmi2CallbackFunctions *functions,
fmi2Boolean visible, fmi2Boolean loggingOn);
typedef void* fmi2Component;
Matlab code
Call the function fmi2Instantiate and create the component.
fmu.component=calllib(model, 'fmi2Instantiate', libpointer('int8Ptr', fmu.instanceName), fmu.type, libpointer('int8Ptr', fmu.guid), libpointer('int8Ptr', resourceLocation), fmu.callbackFunctions, visible, loggingOn);
This component will be further pass in argument to an other C-function.
struct a
,struct b
, and the c-functions and how you want to call these from matlab would help. 2) Why to absolutely avoid having a mex-file make the glue and handle the c-lib "as is" for you ? – CitizenInsanemexCallMATLAB
... humm... let's read for added details ;) – CitizenInsane