Declarations
The Declarations node in the application tree is used to define global variables and objects. These work alongside the global parameters and variables that are already defined in the model.
Variables created under the Declarations node can be referenced in both form objects and methods:
If you only need variables that are internal to a method and not shared with other form objects or methods, you do not need to declare them under the Declarations node.
For information on how to access global parameters defined in the model tree, see Accessing a Global Parameter.
You can also create a Declarations node that is local to a form. Local declarations are available only within that form and can be used in its form objects and methods.
These are the different types of global Declarations:
Form Declarations can only be of the types:
Right-click a Declarations node to access the declaration types or use the ribbon.
Note that Shortcuts are not created from this menu but by clicking the Create Shortcut button next to the Name in the Settings window of a form object or by using Ctrl+K for a selected form object.
To create Declarations that are local to a form, right-click the corresponding form and select the variable type, as shown below.
Variables that are local to a form are organized under a Declarations node that is a child node to the form, as shown below.
The first three types of declarations, Scalar, Array 1D, and Array 2D, can be of the following data types:
In addition to right-clicking the Declarations node, you can click the Create New Declaration and Use It as Source button in the Source section of many types of form objects.
This will open a dialog that lets you quickly declare scalar variables.
Using Declarations as Input Arguments to Commands
Certain commands used in the commands sequence of, for example, a button can take an input argument. For more information, see Button and Item.
The figure below shows a command sequence that includes a Plot Temperature command with an input argument form1/graphics.
You can use declarations as input arguments to commands.
To use a scalar variable, 1D array, or 2D array as input arguments, you use the corresponding variable name. To access a single element of an array, or a row or column of a 2D array, use indexes. For example, to access the first component in a 1D array my_variable, you use my_variable(1). A 2D array element can be retrieved as a scalar by using two indexes, for example, my_matrix(2,3). The indexes can themselves be other declared variables, for example, my_variable(n).
For commands requiring a graphics object as an input argument, only string type declarations are allowed with appropriate indexes, if necessary. If there is a graphics object named graphics1 and also a string declaration named graphics1, then the contents of the string declaration will be used. An exception is if single quotes are used, such as 'graphics1', in which case the graphics object graphics1 is used. This rule is also applied to other combinations of commands and input arguments.
The Name of a Variable
The Name of a variable is a text string without spaces. The string can contain letters, numbers, and underscores. The reserved names root and parent are not allowed and Java® programming language keywords cannot be used.