Main Content

Custom State Attributes in Discrete FIR Filter block

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.

Consider a simple model that contains the Discrete FIR Filter block.

Discrete FIR Filter block connected to an inport and an outport.

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 Data Code Interface Configuration for Model Interface Elements (Simulink Coder).

Open Model Data Editor

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 Class to ExportedGlobal. You can alternatively select the Resolve check box, which requires the state name to resolve to a Simulink® signal object.

Model Data Editor opened below the model canvas with the States tab selected. There is one row in the table for the Discrete FIR Filter block. The Name is set to myState, the Storage Class is set to ExportedGlobal and the Resolve check box is cleared.

Build the Model and Inspect the Generated Code

This example configures the model to generate code only. Open the Configuration Parameters by clicking the configuration button Gear icon. 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 html 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).

A Code Generation Report. On the left is a Contents pane with links to each section of the report, and a Generated Files section with links to each source code file. The pane on the right displays the report. The report is open to the Summary page.

In the custom_state_attributes.h file, you can see that the filter state is declared as an external variable since the storage class is ExportedGlobal.

Code generation report with the header file displayed and the filter state declaration highlighted.

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.

See Also

Tools

Blocks

Related Topics