Main Content

Generate Code for Export-Function Model with Rate-Based Model

You can generate code from a model that includes both function-call subsystems and scheduled subsystems. Create a rate-based model with the scheduled subsystems. Then, reference the rate-based model from an export-function model by using a Model block.

Create Export-Function Model with Scheduled Subsystems and Rate-Based Model

  1. Create a rate-based model with scheduled subsystems. See Create Rate-Based Model.

    In this example, the Sample time for the scheduled subsystem is set to 0.01.

    Rate-based model with scheduled subsystem

  2. Create an export-function model with function-call subsystems. See Create Export-Function Model.

    Export-function model with function-call subsystem

  3. Include the rate-based model in the export-function model by referencing the rate-based model from a Model block.

    Export-function model with function-call subsystem and referenced rate-based model

  4. Display periodic event ports on the Model block by selecting the Schedule rates block parameter.

    Connect Inport blocks to the periodic event ports.

  5. For Inport blocks connected to periodic event ports, select the Output function-call block parameter and specify the sample time with the same sample time from the scheduled subsystem.

    In this example, the Sample time for the Inport block named function-call 10ms is set to 0.01.

    Model block that references rate-based model and receives function-call signal at periodic event port

The completed export-function model includes both a function-call subsystem and a scheduled subsystem.

Generate Code for Export-Function Model with Rate-Based Model

When you generate code for an export-function model that references a rate-based model, the generated code for the rate-based model is integrated into the generated code for the export-function model.

Optionally, open the completed export-function model named export_function_model_with_rate_based_model.

To generate code for the exported functions:

  1. In the Simulink® Toolstrip, on the Modeling tab, click Model Settings. The Configuration Parameters dialog box opens.

  2. On the Solver pane, set Type to Fixed-step.

  3. On the Code Generation pane, set System target file to ert.tlc, which requires an Embedded Coder® license.

  4. In the Simulink Toolstrip, on the Apps tab, in the Apps gallery, select Embedded Coder.

  5. On the C Code tab that opens, click Generate Code.

In the Code Generation Report dialog box that opens, under Code > Model files, select the C file.

In the generated code, each root-level function-call Inport block generates a void-void function. The function name is the name of the output signal from the block. If there is no signal name, then the function name is derived from the name of the block. In this example, the function name was derived from the block name.

Related Topics