| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop Embedded Coder |
| Contents | Index |
| Learn more about Real-Time Workshop Embedded Coder |
| On this page… |
|---|
Initializing Bus Signals in Simulink |
Unlike scalar and vector signals, buses provide no direct way to initialize signals. This section describes techniques for initializing bus signals in Simulink, Stateflow, and Embedded MATLAB.
In Simulink, you can set initial values on a bus by using a set of conditionally executed subsystems, such as Function-Call subsystems, and a Merge block, as shown in this example:

Both subsystems (InitBus and StandardUpdate) create a bus signal of type CounterBus. However, the assignment to the variable GlobalCounter is controlled by the Merge block. See Function-Call Subsystems for more information.
This technique is limited because the StandardUpdate subsystem does not use the initial values from the InitBus subsystem. If the calculations depend on past information from the bus, consider using Stateflow or Embedded MATLAB to initialize bus signals.
Stateflow and Embedded MATLAB allow for conditional execution internally. In the following example, the init and update code are Functions in the Stateflow diagram. This technique simplifies the presentation in the generated code:

In the generated code, you can see that the UpdateCntfunction uses the past value of GlobalCounter.cnt:

The previous example used Stateflow Graphical functions to initialize and update the buses. Alternatively, you can use Embedded MATLAB functions or Simulink subsystems embedded in a Stateflow diagram. The next figure illustrates this technique:

The Simulink subsystems are the same as those used in the earlier Simulink-only example.
The code for specifying a bus of constant values will appear in either the Init or the Step function of the model. The code location depends on the configuration of the bus. In most cases the code appears in the Step function. However if the following conditions hold the code will be placed in the Init function:
The bus is a virtual bus
All signals have the same data type
The signals in the bus are all constants
In the next figure, only the bus named Bus_2 meets all the requirements:

The code for Bus_2 therefore appears in the Init function. The code for the other buses appears in the Step function:

To avoid repeatedly updating a bus of constants, place the bus code into a function-call subsystem, as described in Initializing Bus Signals in Simulink. When you use this technique, make sure the function-call subsystem is called at the start of execution. See Function-Call Subsystems for more information.
![]() | Using Single-Rate and Multi-Rate Buses | Buses and Atomic Subsystems | ![]() |

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