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.
Best Answer