Why do inlined Global Data Store initial values get initialized to all zeros and then reinitialized to the actual values in the generated code using Simulink Coder 8.2 (R2012a)?

1 view (last 30 days)
I have a model which contains a Stateflow Global Data Store variable tied to a Simulink.Signal (or mpt.Signal) object. This object has its 'InitialValue' field set to a non-zero value -- in my case, this is [1 2 3].
My generated code inlines parameters so that the object associated with this Simulink.Signal is initialized to have the initial value {1, 2, 3} (in C syntax).
What I noticed was that the generated code will initialize the array to be {0, 0, 0}. If I choose a different initial value (for example, {1, 2, 3}), the code will first initialize the array to all zeros and then add in the other initial values.
For example, my code looks as follows:
GlobalDataVector[0] = 0.0;
GlobalDataVector[1] = 0.0;
GlobalDataVector[2] = 0.0;
/* other code here */
GlobalDataVector[0] = 1.0;
GlobalDataVector[1] = 2.0;
GlobalDataVector[2] = 3.0;
Why is this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Apr 2012
This is a bug in Simulink Coder 8.2 (R2012a), in that the value will be first initialized to all zeros in order to ensure that the registers associated with the signal value do not hold garbage values. However, this also occurs when the values should later be initalized.
In order to resolve this wasteful initialization code, you can do the following:
1. Open up the Configuration Parameters menu.
2. Navigate to the "Optimization" section on the left-hand menu.
3. Under the "Code Generation -> Data Initialization" pane, check the "Remove internal data zero initialization" checkbox.

More Answers (0)

Products


Release

R2012a

Community Treasure Hunt

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

Start Hunting!