Main Content

Share Data Between Code Generated from Simulink, Stateflow, and MATLAB

Stateflow® and MATLAB® Coder™ can fully define their data definitions, or they can inherit them from Simulink®. Data definition capabilities include:

  • Inheriting input/output data types and sizes from Simulink.

  • 参数化数据类型和大小。也就是说,数据type and size may be specified as a function of another data's type and size, e.g.,type(y)=type(u)andsize(y)=size(u).

  • Inferred output size and type from Simulink via signal attribute back propagation.

  • Parameter scoped data, which allows referencing Simulink parameters in Stateflow and MATLAB.

Open Example Model

Open the example modelrtwdemo_dynamicio.

open_system('rtwdemo_dynamicio')

Instructions

  1. Compile the model (on theSimulationtab, underPrepare, clickUpdate Model) and note the displayed signal types and sizes.

  2. Change the data type and/or size of the Constant block and recompile the model. Note that the attributes of the signals automatically adapt to the Constant block specification.

  3. Generate and inspect code using the blue buttons in the model. Note thatKis shared by the Gain and sfVerticalSum block.

Notes

  • The data type and size of Stateflow and MATLAB data is inherited from Simulink.

  • The Gain block and the Stateflow chart sfVerticalSum share the Simulink parameterK, which is defined in the MATLAB workspace as a Simulink.Parameter withModel defaultstorage class (i.e.,rtP.Kin the generated code).