| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Stateflow |
| Contents | Index |
| Learn more about Stateflow |
| On this page… |
|---|
The goal of this tutorial is to use Simulink functions in a Stateflow chart to improve the design of a model named sf_temporal_logic_scheduler.

The sf_temporal_logic_scheduler model contains a Stateflow chart and three function-call subsystems. These blocks interact as follows:
The chart broadcasts the output events A1, A2, and A3 to trigger the function-call subsystems.
The subsystems A1, A2, and A3 execute at different rates defined by the chart.
The subsystem outputs feed directly into the chart.
No other blocks in the model access the subsystem outputs.
You can replace function-call subsystems with Simulink functions inside a chart when:
The subsystems perform 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 function-call subsystem blocks in a Simulink model with Simulink functions 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 subsystems into Simulink functions in the chart. | Add Simulink Functions to the Chart |
| 3 | Change the scope of specific chart-level data to Local. | Change the Scope of Chart Data |
| 4 | Replace event broadcasts with function calls. | Update State Actions 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_temporal_logic_scheduler_with_sl_fcns at the MATLAB command prompt. |
Type sf_temporal_logic_scheduler at the MATLAB command prompt. If you simulate the model, you see this result in the scope.

For more information, see Scheduling Subsystems to Execute at Specific Times Using a Temporal Logic Scheduler.
Follow these steps to add Simulink functions to the Temporal Logic Scheduler chart.
In the Simulink model, right-click the A1 block in the lower right corner and select Cut from the context menu.

Open the Temporal Logic Scheduler chart.
In the Stateflow Editor, right-click outside any states and select Paste from the context menu.
Expand the new Simulink function so that the signature fits inside the function box.

Rename the Simulink function from A1 to f1 by entering y = f1(u) in the function box.
Repeat steps 1 through 5 for these cases:
Copying the contents of A2 into a Simulink function named f2.
Copying the contents of A3 into a Simulink function named f3.

In the Model Explorer, change the scope of chart-level data y to Local because the calculation for that data now occurs inside the chart.
In the Stateflow Editor, you can replace event broadcasts in state actions with function calls.
Edit the state actions in FastScheduler and SlowScheduler to call the Simulink functions f1, f2, and f3.

In both states, update each during action as follows.
du: y = u1-y2;
For the on every state actions of FastScheduler and SlowScheduler, define three data. (For details, see Adding Data.)
Add local data y1 and y2 to the chart.
Add output data y3 to the chart.
In the model, connect the outport for y3 to the inport of the scope.
In the Model Explorer, delete output events A1, A2, and A3 and input data u2 because the function-call subsystems no longer exist.
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.
![]() | Tutorial: Defining a Function That Uses Simulink Blocks | Building Targets | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |