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 from the Fixed-Point Converter
or using fiaccel
.
The Fixed-Point Converter always synchronizes global data between MATLAB and the generated MEX function.
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;
You can define global data in the MATLAB global workspace, in a Fixed-Point Converter project, or at the command line. If you do not initialize global data in a project or at the command line, the software looks for the variable in the MATLAB global workspace.
To convert the use_globals
function described
in Declare Global Variables, you
must first define and initialize the global data.
global AR B;
AR = ones(4);
B=[1 2 3];
On the Define Input Types page, after selecting and running a test file, select Yes next to Does this code use global variables.
By default, the Fixed-Point Converter names the first global
variable in a project g
.
Enter the names of the global variables used in your code. After adding a global variable, specify its type.
Click Add global to enter more global variables.
Note
If you do not specify the type, you must create a variable with the same name in the global workspace.
To define global data at the command line, use the fiaccel
-globals
option.
For example, to convert the use_globals
function
described in Declare Global Variables 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
.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.
On the Define Input Types page, after selecting and running a test file, select Yes next to Does this code use global variables.
Enter the name of the global variables used in your code.
Click the field to the right of the global variable.
Select Define Constant Value
.
In the field to the right of the constant global variable, enter a MATLAB expression.
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.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)};
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
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.