MATLAB Answers

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"?

1 view (last 30 days)
Sirius on 3 Sep 2019
Commented: Sirius on 25 Sep 2019
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?


Sign in to comment.

Accepted Answer

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
Open MATLAB Function block and change struct elements according to this picture:
Then you have to change the Bus Object definition within the Bus Editor interface:
After build code from the model you see the new structure order of the elements in the code report:
But these is just a small example and should give you only an idea for your specific design.

More Answers (0)

Translated by