2

How does one pass a Simulink.Parameter structure (which, in my case, is a structure of structures) to a C S-function?

Edit:

Information on Simulink.Parameter

You can create a Simulink.Parameter object this way:

modelParameters = Simulink.Parameter;
modelParameters.Value = myStruct;
modelParameters.CoderInfo.StorageClass = 'ExportedGlobal';

The myStruct value is a regular matlab structure of structures. This is how it looks in my case: enter image description here

This is a special object type for passing parameters to Simulink and I am looking for a mechanism to access it from a C S-function.

Download a MnWE from here.

Edit 2: I read the parameters this way:

modelParameters_T *modelParameters = (modelParameters_T*)mxGetPr(ssGetSFcnParam(S, PARAM_STRUCT));

But I can see why this approach doesn't work - the structure object from Matlab is not similar to a C structure, i.e. is not contiguous in memory and contains other properties too. I think I will cast the Matlab structure to an array and then cast the array in C to my struct definition.

9
  • You can pass a parameter through the block mask into the S-Function, but I'm not sure how that works with hierarchical Simulink.Parameters (I guess you mean with Bus Object type).
    – pmb
    Apr 4, 2014 at 14:33
  • @pmb There's Simulink.Parameter data type. I passed that one through the S-function block mask but then how do I read it in C code? I don't think it's a contiguous memory block because getting a pointer to it and reading the first double does not give the right value.
    – remus
    Apr 4, 2014 at 17:13
  • But the type of your Simulink.Parameter is a Bus Object type... I'm not sure why that doesn't work. I thought Bus Objects were simple C structures.
    – pmb
    Apr 7, 2014 at 6:53
  • You can pass busses as inputs but not as parameters. When setting a bus object as input you have to use ssSetInputPortRequiredContiguous(S, INPUT_PORT_BUS, true); and then you can cast it to a C struct. By defualt these busses are not contiguous in memory. Anyway, I couldn't find function to set the parameters as memory contiguous.
    – remus
    Apr 7, 2014 at 7:07
  • This diverges a bit from your question, but could you post a screenshot of the properties of your Simulink.Parameter struct of structs? I don't quite understand how you did this without Bus Objects (and I'd be interested in using it too!).
    – pmb
    Apr 7, 2014 at 7:57

1 Answer 1

0

mxGetPr is not the right way to access your parameter which is an object type. It is not a struct type. Even if it is a struct type you need to use mxArray API to access struct fields. You need to use something like the following code to access the fields.

mxArray* param = ssGetSFcnParam(S, PARAM_STRUCT);
mxArray* prop = mxGetProperty(param, 0, "Value"); // Get Value property from param object
// If prop is double precision use the following line to get its value
double* prop = *(mxGetPr(prop));

Check out mxArray API in the doc for accessing different types of mxArrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.