Implementation of structures defined as a parameter of (embedded) matlab function in Simulink for code generation applications

1 view (last 30 days)
I know that Simulink structures (i.e., bus type objects aka non-virtual bus signals) are expensive to use in a time-critical application. Also, structures cannot be passed as inputs/outputs to/from matlab functions unless they are defined as non-virtual.
1. So, I was wondering about parameter type structures that are used inside matlab function and treated as constants to increase readability of the code? Are they modeled exactly same as non-virtual bus signals with code generation?
2. Is there a recommended size limit for a structs of this type? (e.g., in C#, it is not recommended to use structs > 16 bytes)

Answers (2)

Mark McBroom
Mark McBroom on 7 Apr 2018
Simulink NonVirtual Buses can be used for both signals and parameters. You are correct that when used for signals, there can sometimes be unnecessary data copies, but for parameters there is no such issue. When Simulink Parameters are defined to be of type Simulink.Bus, Emdedded Coder will create an equivalent C structure in the generated code.
MATLAB function blocks fully support parameter type structures. If you define and use the structure only in the MATLAB function block, the resulting generated code will not product a C structure. If you desire to have a C structure, you will need to create a SImulink.Parameter and Simulink.Bus as described above in the MATLAB workspace and then pass this into the ML Fcn block. I am not aware of any sort of size limit for structures in MATLAB Fcn blocks.
  1 Comment
Gorkem
Gorkem on 7 Apr 2018
Edited: Gorkem on 7 Apr 2018
Thank you.
"If you define and use the structure only in the MATLAB function block, the resulting generated code will not product a C structure."
if a C structure isn't produced, how is it implemented then? is it flattened to an array?
And, I recently noticed that compile time constants loaded with coder.load may serve the same purpose (i.e., increasing the readability of the code with a struct parameter). So, performance-wise, would you recommend going for a parameter type struct or coder.load?

Sign in to comment.


Mark McBroom
Mark McBroom on 8 Apr 2018
the elements of the struct will be treated as individual variables in the generated code. Scalar variables will be inlined.
You can use structures with coder.load also. The benefit of coder.load is that you don't have to declare the parameter values in the body of the MATLAB Fcn block, but rather in an external file. This is useful if you expect the values of the parameters to change. If the values are fixed, then it is easier to simply define the parameter values in the ML Fcn block.

Categories

Find more on Multicore Processor Targets in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!