Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Understanding Mask Code Execution

Specifying Mask Initialization

Drawing Commands Execution

Simulink executes the drawing commands in the Drawing Commands pane in the sequence in which the commands appear whenever you:

See Understanding Mask Code Execution for information about the execution order of different types of code in a block mask.

Mask Editor Initialization Pane

When the block is referenced within or copied into a model, the specified default values appear on the block's Mask Parameters dialog box. You cannot use mask initialization code to change mask parameter default values in a library block or any other block.

The Initialization pane enables you to specify initialization commands (see ).

This example demonstrates how to create an improved icon for the mx + b sample masked subsystem discussed earlier in this section. First you must enter the following initialization commands to define the data that enables the drawing command to produce an accurate icon regardless of the shape of the block:

pos = get_param(gcb, 'Position');
width = pos(3) - pos(1); height = pos(4) - pos(2);
x = [0, width];
if (m >= 0), y = [0, (m*width)]; end
if (m < 0),  y = [height, (height + (m*width))]; end

The drawing command that generates this icon is plot(x,y).

Use the Mask Editor Initialization pane to specify block mask initialization commands:

Reference information about the Initialization pane appears in XREF.

Dialog variables

The Initialization pane includes the following controls.

Use the Mask Editor Initialization pane to enter MATLAB commands that initialize a masked block:

The Dialog variables list displays the names of the variables associated with the subsystem's mask parameters, i.e., 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.

Initialization commands

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 Command Window. For information on debugging initialization commands, see .

Summary of Initialization Command Execution

When you open a model, initialization commands execute for all masked blocks that are visible because they reside at the top level of the model or in an open subsystem. Initialization commands for masked blocks that are not initially visible execute when you open the subsystem or model that contains the blocks.

When you load a model into memory without displaying the model graphically, no initialization commands initially run for any masked blocks. See Loading a Model and load_system for information about loading a model without displaying it.

Initialization commands for all masked blocks in a model run when you:

Initialization commands for an individual masked block run when you:

Initialization Command Limitations

Mask initialization commands must observe the following rules:

Callback

Enter the MATLAB code that you want the Simulink software to execute when a user applies a change to the selected parameter, i.e., selects the Apply or OK button on the Mask Parameters dialog box. You can use such callbacks to create dynamic dialogs, i.e., dialogs whose appearance changes, depending on changes to control settings made by the user, e.g., enabling an edit field when a user checks a check box (see more information).

The callback can create and reference variables only in the block's base workspace. If the callback needs the value of a mask parameter, it can use get_param to obtain the value, e.g.,

if str2num(get_param(gcb, 'g'))<0
	error('Gain is negative.')
end

The Simulink software executes the callback commands when you

For information on debugging dialog callbacks, see .

Mask Initialization Code

The initialization code is MATLAB code that you specify and that the Simulink software runs to initialize the masked subsystem at critical times, such as model loading and the start of a simulation run (see ). You can use the initialization code to set the initial values of the masked subsystem's mask parameters.

Debugging Masks that Use M-Code

You can use MATLAB tools to debug mask initialization commands and dialog callbacks in the Mask Editor or the MATLAB Editor/Debugger.

You can debug initialization commands and parameter callbacks entered directly into the Mask Editor in these ways:

You can debug initialization commands and parameter callbacks written in M-files using the MATLAB Editor/Debugger in the same way that you would with any other M-file. When debugging M-file initialization commands, you can view the contents of the mask workspace. When debugging M-file parameter callbacks, you can access only the block's base workspace. If you need the value of a mask parameter, use the get_param command.

You cannot debug icon drawing commands using the MATLAB Editor/Debugger. Use the syntax examples provided in the Mask Editor's Icon pane to help solve errors in the icon drawing commands.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS