Documentation

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, before building the project, you must add a global type definition and initial value for each global variable. 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.

For MEX functions, if you use global data, you must also specify whether to synchronize this data between MATLAB and the MEX function.

Specify a Global Variable Type

  1. Specify the type of each global variable using one of the following methods:

  2. Define an initial valuefor 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.

定义一个全局变量

  1. Click the field to the right of the global variable that you want to define.

  2. SelectDefine by Example.

  3. 在事业中d to the right of the global name, enter a MATLAB expression that has the required class, size, and complexity.MATLAB Coder™software uses the class, size, and complexity of the value of this expression as the type for the global variable.

  4. 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, select10.

    • 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

  1. Click the field to the right of the global variable that you want to define.

  2. Optionally, for numeric types, selectComplexto make the parameter a complex type. By default, inputs are real.

  3. Select the type for the global variable. For example,double.

    By default, the global variable is a scalar.

  4. 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, select10.

    • 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

  1. Click the field to the right of the global variable.

  2. SelectDefine Initial Value.

  3. 输入一个MATLAB表达式。MATLAB Codersoftware 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 Coderuses 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.

  • SelectDefine Initial Value.

  • 输入一个MATLAB表达式。MATLAB Codersoftware uses the value of the specified MATLAB expression as the value of the global variable.

    The project shows that the global variable is initialized.

Define Global Variable Constant Value

  1. Click the field to the right of the global variable.

  2. SelectDefine Constant Value.

  3. 在事业中d to the right of the global variable, enter a MATLAB expression.

Remove Global Variables

  1. Right-click the global variable.

  2. From the menu, selectRemove Global.