Initialize Signals and Discrete States
Simulink® allows you to specify the initial values of signals and discrete states, i.e., the values of the signals and discrete states at the Start time of the simulation. You can use signal objects to specify the initial values of any signal or discrete state in a model. In addition, for some blocks, e.g., Outport, Data Store Memory, or Memory, you can use either a signal object or a block parameter or both to specify the initial value of a block state or output. In such cases, Simulink checks to ensure that the values specified by the signal object and the parameter are consistent. For information about initializing bus signals, see Specify Initial Conditions for Bus Elements
When you specify a signal object for signal or discrete state initialization, or a variable as the value of a block parameter, Simulink resolves the name that you specify to an appropriate object or variable, as described in Symbol Resolution.
A given signal can be associated with at most one signal object under any
circumstances. The signal can refer to the object more than once, but every
reference must resolve to exactly the same object. A different signal object that
has exactly the same properties will not meet the requirement for uniqueness. A
compile-time error occurs if a model associates more than one signal object with
any signal. For more information, see
Simulink.Signal and the Merge block.
Using Block Parameters to Initialize Signals and Discrete States
For blocks that have an initial value or initial condition parameter, you can use that parameter to initialize a signal. For example, the following Block Parameters dialog box initializes the signal for a Unit Delay block with an initial condition of 0.
To access these block parameters, choose one of these techniques:
Use the Model Data Editor (on the Modeling tab, click Model Data Editor) to configure multiple signals and states with a searchable, sortable table. To initialize a block state or data store, you can use the appropriate tab (States or Data Stores). To initialize a signal, state, or data store, you can use the Parameters tab and find the row that corresponds to the relevant block parameter.
Use the Property Inspector (on the Modeling tab, under Design, click Property Inspector) to configure one signal or state at a time. Select the block that maintains the target state or generates the target signal and find the relevant block parameter.
Use the block parameter dialog box. Use this technique to configure one signal or state at a time or to compare the configurations of a few signals or states side by side.
For more information about techniques to access block parameters (including the parameters that control signal and state initialization), see Set Properties and Parameters.
Use Signal Objects to Initialize Signals and Discrete States
You can use signal objects that have a storage class other than
'auto' or, when you set the default storage class
of the corresponding data category to
default setting) in the Code Mapping Editor,
default' to initialize:
Discrete states with an initial condition parameter
Signals in a model except bus signals and blocks that output constant value
To specify an initial value, use the Model Explorer or MATLAB® commands to do the following:
Create the signal object.
On the Model Explorer toolbar, select Add > Simulink Signal. The signal object appears in the base workspace with a default name. Rename the object as
S1. Alternatively, use this command at the command prompt:
S1 = Simulink.Signal;
The name of the signal object must be the same as the name of the signal that the object is initializing. Although not required, consider setting the Signal name must resolve to Simulink signal object option in the Signal Properties dialog box. This setting makes signal objects in the MATLAB workspace consistent with signals that appear in your model.
Consider using the Data Object Wizard to create signal objects. The Data Object Wizard searches a model for signals for which signal objects do not exist. You can then selectively create signal objects for multiple signals listed in the search results with a single operation. For more information about the Data Object Wizard, see Create Data Objects for a Model Using Data Object Wizard.
Set the signal object storage class to a value other than
Model default. In the Model Explorer Contents pane, select the signal object. In the Dialog pane, set Storage class to
ExportedGlobal. Alternatively, use this command at the command prompt:
S1.CoderInfo.StorageClass = 'ExportedGlobal';
Set the initial value. You can specify a MATLAB expression, including the name of a workspace variable, that evaluates to a numeric scalar value or array.
The Simulink engine converts the initial value so the type, complexity, and dimension are consistent with the corresponding block parameter value. If you specify an invalid value or expression, an error message appears when you update the model.
In the Model Explorer Dialog pane, set Initial value to
0.5. Alternatively, use this command at the command prompt:
S1.InitialValue = '0.5'
If you can also use a block parameter to set the initial value of the signal or state, you should set the parameter either to empty (
) or to the same value as the initial value of the signal object. If you set the parameter value to empty, Simulink uses the value specified by the signal object to initialize the signal or state. If you set the parameter to any other value, Simulink compares the parameter value to the signal object value and displays an error if they differ.
The following example shows a signal object specifying the initial output of an enabled subsystem.
sis initialized to 4.5. To avoid a consistency error, the initial value of the enabled subsystem Outport block must be
Create model callback by using PreLoadFcn callback parameter for signal
s. For more information about model callbacks in Simulink, see Model Callbacks.
If you need a signal object and its initial value setting to persist across Simulink sessions, see Create Persistent Data Objects.
Some initial value settings may depend on the initialization mode. For more information, see Underspecified initialization detection.
Classic initialization mode: In this mode,
initial value settings for signal objects that represent the following
signals and states override the corresponding block parameter initial values
if undefined (specified as
Output signals of conditionally executed subsystems and Merge blocks
For more information, see Classic Initialization Mode.
Simplified initialization mode: In this mode, initial values of signal objects associated with the output of the following blocks are ignored. The initial values of the corresponding blocks are used instead.
Output signals of conditionally executed subsystems
For more information about when to use Simplified Initialization Mode and how to configure your model to use this initialization mode, see Simplified Initialization Mode.
You can change the Underspecified initialization
detection settings of your model from
Simplified. For more information, see Convert from Classic to Simplified Initialization Mode.
Using Signal Objects to Tune Initial Values
Simulink allows you to use signal objects as an alternative to
parameter objects (see
tune the initial values of block outputs and states that can be specified
via a tunable parameter. To use a signal object to tune an initial value,
create a signal object with the same name as the signal or state and set the
signal object initial value to an expression that includes a variable
defined in the MATLAB workspace. You can then tune the initial value by changing the
value of the corresponding workspace variable during the simulation.
For example, suppose you want to tune the initial value of a Memory block state named
M1. To do this, you might create a signal object
M1, set its storage class to
'ExportedGlobal', set its initial value to
K is a workspace variable in the MATLAB workspace, and set the corresponding initial condition
parameter of the Memory block to
avoid consistency errors. You could then change the initial value of the
Memory block state any time during the simulation by
changing the value of
K at the MATLAB command line and updating the block diagram (e.g., by typing
Initialization Behavior Summary for Signal Objects
The following model and table show different types of signals and discrete states that you can initialize and the simulation behavior that results for each.
|Signal or Discrete State||Description||Behavior|
|S1||Root input port||
|X1||Unit Delay block — Block with a discrete state that has an initial condition||
|X2||Data Store Memory block||
|S2||Output of an enabled subsystem||
- Simplified Initialization Mode
- Identity Transformation Can Change Model Behavior
- Inconsistent Output with Discrete-Time Integrator or S-Function Block
- Execution Order Affecting Merge Block Output
- Classic Initialization Mode
- Convert from Classic to Simplified Initialization Mode
- Using Initialize, Reinitialize, Reset, and Terminate Functions
- Set Block Parameter Values
- Specify Initial Conditions for Bus Elements
- Signal Basics
- Investigate Signal Values