Main Content

Convert Code Containing Global Data to Fixed Point

Workflow

To convert MATLAB® code that uses global data to fixed-point:

  1. Declare the variables as global in your code.

    For more information, see Declare Global Variables

  2. Before using the global data, define and initialize it.

    For more information, see Define Global Data.

  3. Convert code to fixed-point using fiaccel or codegen (MATLAB Coder).

Automated fixed-point conversion always synchronizes global data between MATLAB and the generated MEX function.

Declare Global Variables

When using global data, you must first declare the global variables in your MATLAB code. This code shows the use_globals function, which uses two global variables, AR and B.

function y = use_globals(u)
%#codegen
% Declare AR and B as global variables
global AR;
global B;
AR(1) = u + B(1);
y = AR * 2;

Define Global Data

You can define global data in the MATLAB global workspace, or at the command line. If you do not initialize global data at the command line, the software looks for the variable in the MATLAB global workspace.

Define Global Data in the MATLAB Global Workspace

To convert the use_globals function, you must first define and initialize the global data.

global AR B;
AR = ones(4);
B=[1 2 3];

Define Global Data at the Command Line

To define global data at the command line, use the fiaccel -globals option. For example, to convert the use_globals function to fixed-point, specify two global inputs, AR and B, at the command line. Use the -args option to specify that the input u is a real, scalar double.

fiaccel -float2fixed cfg -global {'AR',ones(4),'B',[1 2 3]} use_globals -args {0}
Alternatively, specify the type and initial value with the -globals flag using the format -globals {'g', {type, initial_value}}.

To provide initial values for variable-size global data, specify the type and initial value with the -globals flag using the format -globals {'g', {type, initial_value}}. For example, to specify a global variable g that has an initial value [1 1] and upper bound [2 2], enter:

fiaccel -float2fixed cfg -global {'g', {coder.typeof(0,[2 2],1),[1 1]}} myfunction
For a detailed explanation of the syntax, see coder.typeof.

Define Constant Global Data

If you know that the value of a global variable does not change at run time, you can reduce overhead in the fixed-point code by specifying that the global variable has a constant value. You cannot write to the constant global variable.

Define Constant Global Data at the Command Line

To specify that a global variable is constant using the fiaccel command, use the -globals option with the coder.Constant class.

  1. Define a fixed-point conversion configuration object.

    cfg = coder.config('fixpt');
    

  2. Use coder.Constant to specify that a global variable has a constant value. For example, this code specifies that the global variable g has an initial value 4 and that global variable gc has the constant value 42.

    global_values = {'g', 4, 'gc', coder.Constant(42)};
  3. Convert the code to fixed-point using the -globals option. For example, convert myfunction to fixed-point, specifying that the global variables are defined in the cell array global_values.

    fiaccel -float2fixed cfg -global global_values myfunction

Constant Global Data in a Code Generation Report

The code generation report provides this information about a constant global variable:

  • Type of Global on the Variables tab.

  • Highlighted variable name in the Function pane.

See Also

Topics