Main Content

Common Uses of the Initialize Function Block

This example shows how to use the Initialize Function block to perform these common initialization actions.

  • Initialize a block state.

  • Initialize a block parameter.

  • Initialize a data store created using a Data Store Memory block.

The initial value can be:

  • Computed by a block in the model

  • Provided by an external input to the model

  • Provided by external functions in the target environment of the model

This example uses two models. The model initializeFunctionUses demonstrates common use cases. The model initializeFunctionUsesEnv provides a mock target environment in which the first model operates.

Open Use Case Model

Open the model initializeFunctionUses.

open_system('initializeFunctionUses.slx')

The initializeFunctionUses model.

Initialize Block State

The Initialize Block State subsystem initializes a Unit Delay block state where the initial value of the state is computed by another block. This subsystem implements a counter using the Unit Delay block.

Open the Initialize Block State subsystem.

open_system('initializeFunctionUses/Initialize Block State')

The Initialize Block State subsystem.

The Initialize Function block initializes the Unit Delay block state to 5 using a Constant block via a State Writer block. This value overrides the value of the Initial condition parameter of the Unit Delay block, which is 0 in this example.

Open the Initialize Function block inside Initialize Block State subsystem.

open_system('initializeFunctionUses/Initialize Block State/Initialize Function')

The contents of the Initialize Function block inside Initialize Block State subsystem.

Initialize Block Parameter

The Initialize Block Parameter subsystem initializes a block parameter with a value that is dependent on an external input to the model. This subsystem initializes a Gain block parameter with a constant value of 10 or 5 depending on the boolean external signal Use High Gain. The new Gain value overrides the existing Gain value of 1.

Open Initialize Block Parameter subsystem.

open_system('initializeFunctionUses/Initialize Block Parameter')

The Initialize Block Parameter subsystem.

The Initialize Function block uses a Parameter Writer block to write to the Gain block parameter when the subsystem is initialized.

Open the Initialize Function block in the Initialize Block Parameter subsystem.

open_system('initializeFunctionUses/Initialize Block Parameter/Initialize Function')

The contents of the Initialize Function block inside Initialize Block Parameter subsystem.

Initialize Data Store

The Initialize Data Store Memory subsystem initializes a data store with a value that is derived by calling an external function in the target environment for the model. The subsystem implements a counter using the data store blocks. The Data Store Read2 block must be configured to execute first to ensure the count value (DSM Count) is logged before the data store value increases by 1.

Open the Initialize Data Store Memory subsystem.

open_system('initializeFunctionUses/Initialize Data Store Memory')

The Initialize Data Store Memory subsystem.

To configure the Data Store Read2 block to execute first, in the Block Properties dialog box, on the General tab, set Execution Order property to First. For more information, see Specify Block Properties.

Open the Block Properties dialog box for Data Store Read2 block.

open_system('initializeFunctionUses/Initialize Data Store Memory/Data Store Read2','property')

The Block Properties dialog box for the Data Store Read2 block.

Close the Block Property dialog box.

close_system('initializeFunctionUses/Initialize Data Store Memory/Data Store Read2')

Upon initialization, the Initialize Function block initializes the Data Store Memory block inside Initialize Data Store Memory subsystem.

Open the Initialization Function block in the Initialize Data Store Memory subsystem.

open_system('initializeFunctionUses/Initialize Data Store Memory/Initialize Function')

The contents of the Initialize Function block inside Initialize Data Store Memory subsystem.

Upon termination, the Data Store Write block inside the Terminate Function block writes the count value to the Data Store Memory block inside Initialize Data Store Memory subsystem.

Open the Terminate Function block in the Initialize Data Store Memory subsystem.

open_system('initializeFunctionUses/Initialize Data Store Memory/Terminate Function')

The contents of the Terminate Function block inside Initialize Data Store Memory subsystem.

Open Environment Model

The environment model initializeFunctionUsesEnv uses external functions LoadCount and SaveCount because the data store in the Initialize Data Store Memory subsystem call those functions to load and save the counter values from a persistent storage MockExternalStorage created using a Data Store Memory block in initializeFunctionUsesEnv.

Open the initializeFunctionUsesEnv model.

open_system('initializeFunctionUsesEnv.slx')

The initializeFunctionUsesEnv model.

A Model block in the environment model initializeFunctionUsesEnv references the use case model initializeFunctionUses. To enable the initialize and terminate ports of the Model block, select Show model initialize port and Show model terminate port. This setting allows multiple cycles of initialization and termination during simulation.

Open the Model Block Parameters dialog box.

open_system('initializeFunctionUsesEnv/Model','parameter')

The Block Parameters dialog box for the Model block.

Close the Model Block Parameters dialog box.

close_system('initializeFunctionUsesEnv/Model')

At the start of simulation, LoadCount is called to load the initial count value from the persistent storage and upon termination SaveCount is called to save the count value in the persistent storage MockExternalStorage.

Mock Environment Functions

Two Simulink Function blocks define two functions in the environment model. A Data Store Memory block is represents the mock external storage in the environment model.

The LoadCount function loads the count value from the data store MockExternalStorage to the data store inside the Data Store Memory subsystem.

Open the LoadCount function.

open_system('initializeFunctionUsesEnv/Simulink Function1')

LoadCount function.

The SaveCount function saves the count value from the data store inside the Data Store Memory subsystem to the data store MockExternalStorage.

Open the SaveCount function.

open_system('initializeFunctionUsesEnv/Simulink Function2')

SaveCount Function.

Schedule Initialize and Terminate Events

Together, the Function-Call Generator blocks, the Digital Clock block, and logical operator blocks schedule the initialize event at t = 1s and t = 6s, and the terminate event at t = 5s. The environment model (top model) simulates two initialize and terminate cycles for the use case model, which is the referenced model.

  • First cycle: t = 1s to t = 5s

  • Second cycle: t = 6s to t = 10s (simulation stops)

Schedule High and Low Gain Values

The Digital Clock block and a Compare To Constant block provide the boolean input Use High Gain. The use case model initializeFunctionUses uses a low gain value of 5 during the first cycle and a high gain value of 10 during the second cycle.

Simulate the Model and Review Results

Simulate the model and visualize the result using Scope blocks.

sim('initializeFunctionUsesEnv.slx');

Unit Delay Counter

The Scope block named Unit Delay Counter shows the output of the counter implemented using Unit Delay block in the Initialize Block State subsystem. The counter value resets to 5 every time the use case model initializeFunctionUses is initialized.

Open the Scope block named Unit Delay Counter.

open_system('initializeFunctionUsesEnv/Unit Delay Counter')

The output of the Scope block named Unit Delay Counter.

Another Scope block named Amplified Sine Wave shows output of the Initialize Block Parameter subsystem. A sine wave inside the subsystem is amplified by the low gain of 5 during the first initialize and terminate cycle (t = 1s to t = 5s) and by the high gain value of 10 during the second cycle (t = 6s to t = 10s).

Open the Scope block named Amplified Sine Wave.

open_system('initializeFunctionUsesEnv/Amplified Sine Wave')

The output of the Scope block named Amplified Sine Wave.

Another Scope block named DSM Counter shows the output of the counter implemented in the Initialize Data Store Memory subsystem. Unlike the Unit Delay Counter, the count value from DSM Counter persists across all the initialize and terminate cycles.

Open the Scope block named DSM Counter.

open_system('initializeFunctionUsesEnv/DSM Counter')

The output of the Scope block named DSM Counter.

See Also

| | | | | | | | | |

Related Topics