Main Content

Test Export-Function Model Simulation Using Stateflow Chart

Use a Stateflow® chart to provide a function-call scheduler where you can fully control the scheduling process for periodic (synchronous) or aperiodic (asynchronous) call sequences.

  • Create a new Simulink® model.

  • Add a Model block that references the export-function model.

  • Specify function-call Inputs using a Stateflow chart.

  • Specify data inputs.

  • Run simulation.

To create the model for this example, see Create an Export-Function Model.

To open a completed test model, see ex_export_function_test_model_with_stateflow_chart.

Create Referenced Export-Function Model

Referencing an export-function model from a Model block allows the addition of function-calls events from a Stateflow chart and the logging of data signals for testing without changing the model itself.

  1. Add a Model block to a new Simulink model. In the Model name box, enter export_function_model.

  2. Add Outport blocks to the output_100ms and output_10ms ports for saving simulation data to MATLAB.

  3. Add a Sine Wave block to provide data input. Set Amplitude to 2 and Sample time to 0.01. Connect the block to the input_10ms input port on the Model block.

Create Periodic Scheduler Using Stateflow Chart

  1. Create a new Stateflow chart. This step requires a Stateflow license.

  2. On the Modeling tab and from the Design section, select Model Explorer . In the Model Hierarchy pane, select Chart.

  3. Add function-call events with output ports to the chart. From the menu, select Add > Event. In the Name box, enter out_100ms. From the Scope list, select Output to Simulink. Repeat step to create a function-call event and output port for out_10ms.

  4. Open the chart by double-clicking the block. Add a State block and a Default transition arrow.

  5. Rename the state to function_call_scheduler.

  6. Add periodic function-calls every 10 and 100 milliseconds. In the state block, enter the following commands.

    on every(10, tick):send(out_10ms);
    on every(100, tick):send(out_100ms);

    The keyword tick is an implicit event that counts the number of simulation steps while send is an explicit event that outputs a function-call event to the output ports.

Create Test Model (Harness) for Simulation

You use a Simulink test model only for simulation. After simulation testing, generate code from the export-function model, and then integrate exported function code with an externally coded scheduler.

  1. Add a Stateflow chart to your test model.

  2. Connect the Stateflow Chart outputs to Model block inputs.

Scheduling Restrictions for Referenced Export-Function Models

If a test model references an export-function model, there are some restrictions to ensure consistency with simulation results.

For the test model:

  • Function-calls to the input ports on the Model block must follow the execution order of the root-level function-call Inport blocks in the referenced export-function model.

    If the test model calls the referenced model functions out of order at any time step, Simulink displays an error. For information on sorted execution order, see Control and Display Execution Order. To disable this restriction, clear the check box for the configuration parameter Enable strict scheduling checks for referenced models.

For the export-function model:

  • The sample times for the root-level function-call Inport blocks must be set to inherited (-1) or match the sample time of the function-calls from the Stateflow chart that drives them.

Simulate Export Function Model

Simulate the export-function model to test and observe its behavior before generating code.


When using export-function models in top-model simulations, do not change the enable/disable status of the model during simulation. Enable it at the start of simulation and use function-calls to call it.

  1. Set configuration parameters for the test model. On the Modeling tab and from the Setup section, select Model Settings . Select the Model Referencing pane. Clear the check box for the configuration parameter Enable strict scheduling check for referenced models.

  2. Verify the configuration parameters Solver Type is set to Fixed-step, Solver set to discrete (no continuous states) and Fixed-step size (fundamental sample time) set to auto.

  3. Right-click output port signals on the Model block and select Log selected signal.

  4. Run simulation.

  5. Open the Simulation Data Inspector by clicking the icon .

After you test your model, you can generate code for the functions. See Generate Code for Export-Function Model.

See Also

Related Topics