How to specify the orders of signals appeared in the struct of the generated code if the storage class is set to be "Model default" or "Struct"?

4 views (last 30 days)
When the storage class of signals is set to "Model default" or "Struct" in the simulink model for code generation, the corresponding signals will appear as struct fields in the generated code. However, it seems that the order of struct fields cannot be custom and will be chaotic after the model has been modified for many times. I wonder is there any method to specify the struct fileds order in generated code?

Accepted Answer

TecBen
TecBen on 17 Sep 2019
Basically the order of a structure after generating C-Code from a Simulink model can not be sorted in all cases and model designs.
We do not ship an Custom Storage Class (CSC) like "ImportedStruct" because we do not want to make the HeaderFile instance-specific (because this would force users to specify the same HeaderFile name on all of the data objects that belong to the same structure, which could be in certain cases cumbersome, error prone and complicated).
But you can define your own ImportedStruct CSCs and define the HeaderFile as part of the CSC definition with with the Custom Storage Class designer (you need a Embedded Coder license for the cscdesigner tool):
With the Simulink Coder product you can change the order of struct elements directly by using the demo model "emldemo_bus_struct" as a template which is described in more details in top of this page:
After changing the order of the structure in the MATLAB function block and the definition in the Bus Editor (see attached pictures) you can change the order of the typedef struct {...} MainBus in the generated C-Code.
You can reproduce design by opening the demo model from MATLAB command window by type >> emldemo_bus_struct
slc_struct_sorted_order_demo_i1.jpg
Open MATLAB Function block and change struct elements according to this picture:
slc_struct_sorted_order_demo_i2.jpg
Then you have to change the Bus Object definition within the Bus Editor interface:
slc_struct_sorted_order_demo_i4.jpg
After build code from the model you see the new structure order of the elements in the code report:
slc_struct_sorted_order_demo_i3.jpg
But these is just a small example and should give you only an idea for your specific design.

More Answers (0)

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!