Convert Code Containing Global Data to Fixed Point
Workflow
To convert MATLAB® code that uses global data to fixed-point:
Declare the variables as global in your code.
For more information, see Declare Global Variables
Before using the global data, define and initialize it.
For more information, see Define Global Data.
Convert code to fixed-point using
fiaccelorcodegen(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}
-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
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.
Define a fixed-point conversion configuration object.
cfg = coder.config('fixpt');Use
coder.Constantto specify that a global variable has a constant value. For example, this code specifies that the global variableghas an initial value4and that global variablegchas the constant value42.global_values = {'g', 4, 'gc', coder.Constant(42)};Convert the code to fixed-point using the
-globalsoption. For example, convertmyfunctionto fixed-point, specifying that the global variables are defined in the cell arrayglobal_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
Globalon the Variables tab.Highlighted variable name in the Function pane.