The initialization code is MATLAB® code that you specify and that Simulink® runs to initialize the masked subsystem at critical times, such as model loading and the start of a simulation run (see Initialization Command Execution). You can use the initialization code to set the initial values of the mask parameters.
The masked subsystem initialization code can refer only to variables in its local workspace.
When you reference the block within, or copy the 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.
Use the Mask Editor Initialization pane to enter MATLAB commands that initialize a masked block. Reference information about the Initialization pane appears in Initialization Pane.
The Initialization pane has two sections:
Dialog variables list
Initialization commands edit area
The Dialog variables list displays the names of the variables associated with the mask parameters of the masked block (that is, the parameters defined in the Parameters pane).
You can copy the name of a parameter from this list and paste it into the adjacent Initialization commands field, using the Simulink keyboard copy and paste commands.
You can also use the list to change the names of mask parameter variables. To change a name, double-click the name in the list. An edit field containing the existing name appears. Edit the existing name and press Enter or click outside the edit field to confirm your changes.
Enter the initialization commands in this field. You can enter any 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.
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 Create Dynamic Mask Dialog Boxes.
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.