You can add MATLAB® code in the Initialization pane of the 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. For more information, see Execute Initialization Command.
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 = 5in the Initialization pane.
To specify the 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 Dynamic Masked Subsystem.
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.
Ensure that the mask parameters used in the mask initialization code are defined. Errors in mask initialization are displayed when editing the mask initialization commands, but this is only possible if all the mask parameter values are evaluated without errors.
Avoid unconditionally modifying the contents of a subsystem from the mask initialization code. Such unconditional modification does not work properly when using model reference.
When you use
set_paramin the mask initialization code of a Subsystem block, all the child blocks are also evaluated.
When you use a
Simulink.Parameterobject as the value of a mask parameter, if the value of the object is a numeric value, the mask initialization code can treat the mask parameter as a MATLAB variable.
Use the Mask Editor Code pane to add MATLAB commands that initialize a masked block.
The Code pane contains these sections:
Initialization & Callback
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 Code 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.
To avoid echoing results to the MATLAB Command Window, terminate initialization commands with a semicolon.
To view related examples, see
Mask Initialization Best Practices
Mask initialization commands must observe the following rules:
Do not use initialization code to create dynamic mask dialog boxes (Dialog boxes whose appearance or control settings change depending on changes made to other control settings). Instead, use the mask callbacks that are intended for this purpose. For more information, see Dynamic Mask Dialog Box.
Do not use initialization code to add or delete blocks during model load time.
For nested masked subsystem, do not use
set_paramon a parent block from a child block. The child block mask and the parent block mask both could be initializing the same parameter of the block leading to unexpected behavior. For more information, see Unsafe Mask Callback Error.
Do not use
set_paramcommands on blocks that reside in another masked subsystem that you are initializing. Trying to set parameters of blocks in child level masked subsystems can trigger unresolved symbol errors if child level masked subsystems reference symbols defined by parent level masked subsystems.
Suppose, for example, a masked subsystem
Acontains a masked subsystem
Bwhich contains Gain block
Gainparameter references a variable defined by
B. Suppose also that subsystem
Ahas initialization code that contains this 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:
Do not add initialization code to delete the same masked block.
Use mask initialization code to control direct child blocks only.
Do not use mask initialization code to comment or uncomment a block.