This example shows how to customize the state attributes of the Discrete FIR Filter block using the Model Data Editor. The Model Data Editor enables you to inspect and edit data items in a list that you can sort, group, and filter. For more information on using the Model Data Editor, see Configure Data Properties by Using the Model Data Editor.
Consider a simple model that contains the Discrete FIR Filter block.
Using the Code Mappings editor or code mappings API, you can configure the state of the
Discrete FIR Filter to appear in the generated code as a separate
global variable. This is done by declaring the storage class of the state as
ExportedGlobal. For details on how the generated code
stores internal states, see How Generated Code Stores Internal Signal, State, and Parameter Data (Simulink Coder). For
more details on storage classes and how to apply them to the states, see C Code Generation Configuration for Model Interface Elements (Simulink Coder).
Open the Model Data Editor. On the Modeling tab, click Model Data Editor.
Under the States tab, enter the Name as
myState. In the coder app, set the Storage
ExportedGlobal. You can
alternatively select the Resolve check box, which requires the
state name to resolve to a Simulink® signal object.
This example configures the model to generate code only. Open the Configuration Parameters by clicking the configuration button in the Simulink editor. In the Code Generation pane, select Generate code only. Click Apply.
In the Report pane, select Create code generation report and Open report automatically. Click Apply. These settings create a report and automatically open the report in a web browser.
To initiate the build, click the build model button in the Simulink editor or press Ctrl+B. The build
process writes the code generation report files to the
subfolder of the build folder. Next, the build process automatically opens a
MATLAB® web browser window and displays the code generation report. Using this
report, you can view and analyze the generated code. For more information on the
generated report, see Reports for Code Generation (Simulink Coder).
custom_state_attributes.h file, you can see that the
filter state is declared as an external variable since the storage class is
If you change the storage class and rebuild the model, you can see the generated code reflect the change. With the ability to customize the state attributes, you can streamline and customize how the state appears in the generated code.