- The parameter (denoted as 'mass' throughout the code) is allocated and stored on the block in mdlSetWorkWidths - look at how the pointer 'mass' is allocated and cached on the block using ssSetRunTimeParamInfo.
- Since the parameter is a function of other dialog parameters, you need to update it every time the parameters change. Hence, you need to have code in mdlSetWorkWidths and in mdlProcessParameters to update the value of 'mass' based on the parameters.
- In mdlOutputs, you need to read the value, so you use (ssGetRunTimeParamInfo(S,0))->data to get the cached value and use it a required.
- Manually free the associated memory in mdlTerminate - you allocated it, so you need to free it as well.
(RunTimeDataType*) notation is a cast? pointer and -> operator for sfunction
2 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I need to figure out the way to read a parameter from a C mex Sfunction, so I pick up some examples. I took the sfun_runtime4.c, used in sfcndemo_runtime.mdl. that you can get from this page http://www.mathworks.nl/help/toolbox/simulink/sfg/rmvd_matlablink__638.html.
I'm studying the mdlProcessParameters method, and it is a little bit tough for me to understand this notation :
RunTimeDataType *mass = (RunTimeDataType*)((ssGetRunTimeParamInfo(S,0))->data);
reading the doc of ssGetRunTimeParamInfo http://www.mathworks.nl/help/toolbox/simulink/sfg/ssupdateruntimeparamdata.html I understood that this method is used in order to obtain the attributes of the run-time parameter specified by 0 index. This method return a pointer, which, I bet, refers to a structure, from which by using the -> operator we get the data attribute. Then I'm not sure what it is supposed to do with (RunTimeDataType*), it makes a kind of cast to convert the value in a RunTimeDataType pointer, doesn't it?
Am I right about it?
e From the same method there is this other part of code which is also tricky for me to understand, I extract a part:
real_T vol = *mxGetPr(pm)
calcMass(mass, vol);
the mxGetPr return a pointer to the first element of the real data and the with * operator we can get the value of the first value, then we call calcMass function, which has as first argument an adress (see above for mass) and a value am I right? or I just have a big mess inside my head?
thanks for reading
thanks in advance for the help
0 个评论
采纳的回答
Kaustubha Govind
2012-3-6
In the example that you are referring to, RunTimeDataType is a user-defined datatype, and it is the datatype of a parameter whose value is a function of multiple S-function parameters. This parameter does not appear on the block dialog at all, but is an internal run-time parameter that depends on other parameters. Simulink does not anything know about this parameter - it only holds the pointer for you, but you need to allocate, maintain and free the memory associated with the parameter data. All responsibility lies with you. I would say that this is a more advanced concept, and unless you need internal run-time parameters, you should simply use ssRegDlgParamAsRunTimeParam if you only want to use a dialog parameter as a run-time parameter.
However, in case you're still interested:
And yes, your understanding of the call to calcMass is correct - I believe this function updates the cached value of 'mass' based on the values passed to it.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Block and Blockset Authoring 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!