You can add MATLAB® code in the initialization pane of Mask Editor to initialize a masked block. Simulink® executes these initialization commands to initialize a masked subsystem at critical times, such as model loading and start of a simulation run (See Initialization Command Execution).
You can add mask initialization code for these cases:
To specify the initial values of mask parameters.
For example to specify an initial value of parameter
a = 5 in the initialization pane.
To specify an acceptable range of inputs for mask
parameter. For example, to specify the range between
the acceptable values for parameter
= a > = 10.
To specify value of a child block. For example,
set_param('Child block Name','Parameter name','Parameter Value')
To create a self-modifiable mask. For more information, see Self-Modifying Mask.
The initialization code of a masked subsystem can refer only to the variables in its local workspace.
When you reference a block with, or copy a block into, a model, the mask dialog box displays the specified default values. You cannot use mask initialization code to change mask parameter default values in a library block or any other block.
Note: Blocks that contain initialization code do not work as expected when using model reference.
Use the Mask Editor Initialization pane to add MATLAB commands that initialize a masked block.
The Initialization pane contains these sections:
The Dialog variables section displays the names of the variables associated with the mask parameters of the masked block that are defined in the Parameters pane.
You can copy the name of a parameter from this list and paste it into the Initialization commands section.
You can change the name of the mask parameter variable in the list by double-clicking and editing the name.
You can add the initialization commands in this section. The initialization code must be a valid MATLAB expression, consisting of MATLAB functions and scripts, operators, and variables defined in the mask workspace. Initialization commands cannot access base workspace variables.
Terminate initialization commands with a semicolon to avoid echoing results to the MATLAB Command Window.
To view related examples, see
Mask initialization commands must observe the following rules:
Do not use initialization code to create dynamic mask dialog boxes (that is, dialog boxes whose appearance or control settings change depending on changes made to other control settings). Instead, use the mask callbacks that are specifically for this purpose. For more information, see Dynamic Mask Dialog Box.
Do not use
set_param commands on
blocks residing in another masked subsystem that you are initializing.
Trying to set parameters of blocks in lower-level masked subsystems
can trigger unresolved symbol errors if lower-level masked subsystems
reference symbols defined by higher-level masked subsystems. Suppose,
for example, a masked subsystem A contains masked subsystem B, which
contains Gain block C, whose Gain parameter references a variable
defined by B. Suppose also that subsystem A has initialization code
that contains the following command:
set_param([gcb '/B/C'], 'SampleTime', '-1');
Simulating or updating a model containing A causes an unresolved symbol error.
You cannot use mask initialization code to create data objects. Data objects are objects of these classes:
Simulink.Parameter and subclasses
Simulink.Signal and subclasses
Do not add initialization code to delete the same masked block.
Use mask initialization code to control direct child blocks only.