| Contents | Index |
| On this page… |
|---|
The goal of this tutorial is to use a Simulink function in a Stateflow chart to improve the design of a model named old_sf_car.

The old_sf_car model contains a function-call subsystem named Threshold Calculation and a Stateflow chart named shift_logic. The two blocks interact as follows:
The chart broadcasts the output event CALC_TH to trigger the function-call subsystem.
The subsystem uses lookup tables to interpolate two values for the shift_logic chart.
The subsystem outputs (up_th and down_th) feed directly into the chart as inputs.
No other blocks in the model access the subsystem outputs.
You can replace a function-call subsystem with a Simulink function in a chart when:
The subsystem performs calculations required by the chart.
Other blocks in the model do not need access to the subsystem outputs.
The sections that follow describe how to replace a function-call subsystem in a Simulink model with a Simulink function in a Stateflow chart. This procedure reduces the number of objects in the model while retaining the same simulation results.
| Step | Task | Reference |
|---|---|---|
| 1 | Open the model. | Open the Model |
| 2 | Move the contents of the function-call subsystem into a Simulink function in the chart. | Add a Simulink Function to the Chart |
| 3 | Change the scope of specific chart-level data to Local. | Change the Scope of Chart Data |
| 4 | Replace the event broadcast with a function call. | Update State Action in the Chart |
| 5 | Verify that function inputs and outputs are defined. | Add Data to the Chart |
| 6 | Remove unused items in the model. | Remove Unused Items in the Model |
Note To skip the conversion steps and access the new model directly, type sf_car at the MATLAB command prompt. |
Type old_sf_car at the MATLAB command prompt. If you simulate the model, you see these results in the two scopes.


Follow these steps to add a Simulink function to the shift_logic chart.
In the Simulink model, right-click the Threshold Calculation block in the lower left corner and select Cut from the context menu.

Open the shift_logic chart.
In the chart, right-click below selection_state and select Paste from the context menu.
Expand the new Simulink function so that the signature fits inside the function box.

Expand the border of selection_state to include the new function.

Note The function resides in this state instead of the chart level because no other state in the chart requires the function outputs up_th and down_th. See How a Simulink Function Binds to a State. |
Rename the Simulink function from Threshold_Calculation to calc_threshold by entering [down_th, up_th] = calc_threshold(gear, throttle) in the function box.
In the Model Explorer, change the scope of chart-level data up_th and down_th to Local because calculations for those data now occur inside the chart.
In the Stateflow Editor, change the during action in selection_state to call the Simulink function calc_threshold.
during: [down_th, up_th] = calc_threshold(gear, throttle);

Because the function calc_threshold takes throttle as an input, you must define that data as a chart input. (For details, see Adding Data.)
Add input data throttle to the chart with a Port property of 1.
Using port 1 prevents signal lines from overlapping in the Simulink model.
In the Simulink model, add a signal line for throttle between the inport of the Engine block and the inport of the shift_logic chart.
In the Model Explorer, delete the function-call output event CALC_TH because the Threshold Calculation block no longer exists.
Delete any dashed signal lines from your model.
Your new model looks something like this:

If you simulate the new model, the results match those of the original design.
![]() | Best Practices for Using Simulink Functions | Scheduling Execution of Multiple Controllers | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |