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.

remus
  • 2,635
  • 2
  • 21
  • 46
  • 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 04 '14 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 04 '14 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 07 '14 at 06: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 07 '14 at 07: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 07 '14 at 07:57
  • @pmb I made an update to the question with code and screenshot. – remus Apr 07 '14 at 09:07
  • Extra comment: for example, in Simulink you can now set the value of a gain this way: modelParameters.Common.Gain. You can also see the modelParameters object in Model explorer. – remus Apr 07 '14 at 09:11
  • How are you accessing the parameter from your s-function? If you are using ssGetSFcnParam it gives you an mxArray. This mxArray would be the object you passed in. You need to use mxArray API to access properties of this object. For example, mxGetProperty. – Navan Apr 09 '14 at 13:58
  • @Navan I added some code in the question body. – remus Apr 11 '14 at 09:34

1 Answers1

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.

Navan
  • 4,407
  • 1
  • 24
  • 26