Specify Global Variable Type and Initial Value Using the App
Why Specify a Type Definition for Global Variables?
If you use global variables in your MATLAB® algorithm, you must add a global type definition and initial value for each global variable before building the project. If you do not initialize the global data, the app looks for the variable in the MATLAB global workspace. If the variable does not exist, the app generates an error.
If you use global data in MEX functions, you must also specify whether to synchronize this data between MATLAB and the MEX function. See Synchronizing Global Data with MATLAB.
Add Global Data
To add global variables:
On the Define Input Types page, automatically define input types or click Let me enter input or global types directly.
The app displays a table of entry-point inputs.
To add a global variable, click Add global. By default, the app names the first global variable in a project
g
, and subsequent global variablesg1
,g2
, and so on.Under Global variables, enter a name for the global variable.
Before generating code, specify the type and initial value. Otherwise, you must create a variable with the same name in the global workspace.
Specify a Global Variable Type and Initial Value
Specify the type of each global variable using one of the following methods:
Define an initial value for each global variable.
If you do not provide a type definition and initial value for a global variable, create a variable with the same name and suitable class, size, complexity, and value in the MATLAB workspace.
Define a Global Variable by Example
Click the field to the right of the global variable that you want to define.
Select Define by Example.
In the field to the right of the global name, enter a MATLAB expression that has the required class, size, and complexity. MATLAB Coder™ uses the class, size, and complexity of the value of this expression as the type for the global variable.
Alternatively, you can select a variable from the list of workspace variables.
Optionally, change the size of the global variable. Click the dimension that you want to change and enter the size, for example,
10
.You can specify:
Fixed size. In this example, select
10
.Variable size, up to a specified limit, by using the
:
prefix. In this example, to specify that your input can vary in size up to10
, select:10
.Unbounded variable size by selecting
:Inf
.
Define or Edit Global Variable Type
Click the field to the right of the global variable that you want to define.
Optionally, for numeric types, select Complex to make the parameter a complex type. By default, inputs are real.
Select the type for the global variable. For example,
double
.By default, the global variable is a scalar.
Optionally, change the size of the global variable. Click the dimension that you want to change and enter the size, for example,
10
.You can specify:
Fixed size. In this example, select
10
.Variable size, up to a specified limit, by using the
:
prefix. In this example, to specify that your input can vary in size up to10
, select:10
.Unbounded variable size by selecting
:Inf
.
Define Global Variable Initial Value
Define Initial Value Before Defining Type
Click the field to the right of the global variable.
Select Define Initial Value.
Enter a MATLAB expression. MATLAB Coder uses the value of the specified MATLAB expression as the value of the global variable. Because you did not define the type of the global variable before you defined its initial value, MATLAB Coder uses the initial value type as the global variable type.
The project shows that the global variable is initialized.
If you change the type of a global variable after defining its initial value, you must redefine the initial value.
Define Initial Value After Defining Type
Click the type field of a predefined global variable.
Select Define Initial Value.
Enter a MATLAB expression. MATLAB Coder uses the value of the specified expression as the value of the global variable. If the type of the initial value differs from the type that you specified before initializing the global variable, MATLAB Coder uses the type of the initial value. The project shows that the global variable is initialized.
Define Constant Global Variable
Click the field to the right of the global variable.
Select Define Constant Value.
In the field to the right of the global variable, enter a MATLAB expression.
Remove Global Variables
Right-click the global variable.
From the menu, select Remove Global.