Main Content

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:

  1. 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.

  2. To add a global variable, click Add global. By default, the app names the first global variable in a project g, and subsequent global variables g1, g2, and so on.

  3. Under Global variables, enter a name for the global variable.

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

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

  2. 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

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

  2. Select Define by Example.

  3. 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.

    App dialog box, showing list of workspace variables. Variable g, a scalar double, is highlighted.

  4. Optionally, change the size of the global variable. Click the dimension that you want to change and enter the size, for example, 10.

    App dialog box, showing where to change the size of variable g

    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 to 10, 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, select Complex to 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.

    App dialog box, showing where to change the size of variable g

    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 to 10, 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. Select Define Initial Value.

  3. 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.

    App window, showing that global variable g 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

  1. Click the type field of a predefined global variable.

  2. Select Define Initial Value.

  3. 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.

    App window, showing that global variable g is initialized

Define Constant Global Variable

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

  2. Select Define Constant Value.

  3. In the field to the right of the global variable, enter a MATLAB expression.

Remove Global Variables

  1. Right-click the global variable.

  2. From the menu, select Remove Global.