| Real-Time Workshop® | ![]() |
| On this page… |
|---|
Why You Must Specify Input Properties Rules for Specifying Properties of Primary Inputs Methods for Defining Properties of Primary Inputs |
Because C is a statically typed language, Embedded MATLAB™ Coder must determine the properties of all variables in the M-files at compile time. To infer variable properties in M-files, Embedded MATLAB Coder must be able to identify the properties of the inputs to the primary function, also known as the top-level or entry-point function. Therefore, if your primary function has inputs, you must specify the properties of these inputs, also called preconditions, to Embedded MATLAB Coder. If your primary function has no input parameters, Embedded MATLAB Coder can compile your M-file without modification. You do not need to specify properties of inputs to subfunctions or external functions called by the primary function.
If your primary function has inputs, you must specify the following properties for each input:
| For: | Specify Properties: | ||||
|---|---|---|---|---|---|
| Class | Size | Complexity | numerictype | fimath | |
| Fixed-point inputs |
|
|
|
|
|
| Each field in a structure input | |||||
| All other inputs |
|
|
| ||
Embedded MATLAB Coder assigns the following default values for properties of primary function inputs:
| Property | Default |
|---|---|
| class | double |
| size | scalar |
| complexity | real |
| numerictype | No default |
| fimath | No default |
Note In most cases, Embedded MATLAB Coder uses defaults when you don't explicitly specify values for properties — except for structure fields. The only way to name a field in a structure is to set at least one of its properties. Therefore, you may need to specify default values for properties of structure fields. For examples, see Example: Specifying Class and Size of Scalar Structure and Example: Specifying Class and Size of Structure Array. |
The following table presents the class names supported by Embedded MATLAB Coder:
| Class Name | Description |
|---|---|
| logical | Logical array of true and false values |
| char | Character array |
| int8 | 8-bit signed integer array |
| uint8 | 8-bit unsigned integer array |
| int16 | 16-bit signed integer array |
| uint16 | 16-bit unsigned integer array |
| int32 | 32-bit signed integer array |
| uint32 | 32-bit unsigned integer array |
| single | Single-precision floating-point or fixed-point number array |
| double | Double-precision floating-point or fixed-point number array |
| struct | Structure array |
| embedded.fi | Fixed-point number array |
Follow these rules when specifying the properties of primary inputs:
For each primary function input whose class is fixed point (fi), you must specify the input's numerictype and fimath properties.
For each primary function input whose class is struct, you must specify the properties of each of its fields in the order that they appear in the structure definition.
Use one of the following methods to define the properties of primary function inputs:
| Method | Pros | Cons |
|---|---|---|
|
| |
| Defining Input Properties Programmatically in the M-File |
|
|
Note To specify the properties of inputs for any given primary function, use one of these methods, but not both. |
The command that invokes Embedded MATLAB Coder — emlc — provides a command-line option -eg for specifying the properties of primary function inputs as a cell array of example values. The cell array can be a variable or literal array of constant values. Using this option, you specify the properties of inputs at the same time as you compile the M-file with Embedded MATLAB Coder.
See -eg Specify Input Properties by Example for emlc in the Real-Time Workshop® Function Reference.
Follow these rules when using the -eg command-line option to define properties by example:
The cell array of sample values must contain the same number of elements as primary function inputs.
The order of elements in the cell array must correspond to the order in which inputs appear in the primary function signature — for example, the first element in the cell array defines the properties of the first primary function input.
You can define inputs to be constants using the-eg option with emlc in the same way as you can with emlmex. See Specifying Constant Inputs in the Embedded MATLAB User's Guide.
Consider an M-function that adds its two inputs:
%#eml function y = emcf(u,v) y = u + v;
The following examples show how to specify different properties of the primary inputs u and v by example at the command line:
Use a literal cell array of constants to specify that both inputs are real scalar doubles:
emlc -o emcfx emcf -eg {0,0}Use a literal cell array of constants to specify that input u is an unsigned 16-bit, 1-by-4 vector and input v is a scalar double:
emlc -o emcfx emcf -eg {zeros(1,4,'uint16'),0}Assign sample values to a cell array variable to specify that both inputs are real, unsigned 8-bit integer vectors:
a = uint8([1;2;3;4])
b = uint8([5;6;7;8])
ex = {a,b}
emlc -o emcfx emcf -eg exConsider an M-function that calculates the square root of a fixed-point number:
%#eml function y = sqrtfi(x) y = sqrt(x);
To specify the properties of the primary fixed-point input x by example on the MATLAB® command line, follow these steps:
Define the numerictype properties for x, as in this example:
T = numerictype('WordLength',32,
'FractionLength',23,
'Signed',true);Define the fimath properties for x, as in this example:
F = fimath('SumMode','SpecifyPrecision',
'SumWordLength',32,
'SumFractionLength',23,
'ProductMode','SpecifyPrecision',
'ProductWordLength',32,
'ProductFractionLength',23);Create a fixed-point variable with the numerictype and fimath properties you just defined, as in this example:
myeg = { fi(4.0,T,F) };Compile the function sqrtfi using the emlc command, passing the variable myeg as the argument to the -eg option, as in this example:
emlc sqrtfi -eg myeg;
Embedded MATLAB Coder lets you use the MATLAB assert function to define properties of primary function inputs directly in your M-file.
Use the assert function to invoke standard MATLAB functions for specifying the class, size, and complexity of primary function inputs.
| Specify Any Class | assert (isa (param, 'class_name')) |
| Specify fi Class | assert (isfi (param)) |
| Specify Structure Class | assert (isstruct (param)) |
| Specify Any Size | assert (all (size(param == [dims])) |
| Specify Scalar Size | assert (isscalar((param)) |
| Specify Real Input | assert (isreal((param)) |
| Specify Complex Input | assert (~isreal((param)) |
| Specify numerictype of Fixed-Point Input | assert (isequal (numerictype(fiparam), T)) |
| Specify fimath of Fixed-Point Input | assert (isequal (fimath(fiparam),F)) |
| Specify Multiple Properties of Input | assert (function1(params) && function2(params) && function3(params) && ...) |
assert ( isa ( param, 'class_name') )
Sets the input parameter param to the MATLAB class class_name. For example, to set the class of input U to a 32-bit signed integer, call:
... assert(isa(U,'int32')); ...
Note If you set the class of an input parameter to fi, you must also set its numerictype and fimath properties (see Specify numerictype of Fixed-Point Input and Specify fimath of Fixed-Point Input. If you set the class of an input parameter to struct, you must specify the properties of all fields in the order that they appear in the structure definition. |
assert ( isfi ( param ) ) assert ( isa ( param, 'embedded.fi' ) )
Sets the input parameter param to the MATLAB class fi (fixed-point numeric object). For example, to set the class of input U to fi, call:
... assert(isfi(U)); ...
or
... assert(isa(U,'embedded.fi')); ...
Note If you set the class of an input parameter to fi, you must also set its numerictype and fimath properties (see Specify numerictype of Fixed-Point Input and Specify fimath of Fixed-Point Input). If you set the class of an input parameter to struct, you must specify the properties of all fields in the order they appear in the structure definition. |
assert ( isstruct ( param ) ) assert ( isa ( param, 'struct' ) )
Sets the input parameter param to the MATLAB class struct (structure). For example, to set the class of input U to a struct, call:
... assert(isstruct(U)); ...
or
... assert(isa(U, 'struct')); ...
Note If you set the class of an input parameter to struct, you must specify the properties of all fields in the order they appear in the structure definition. |
assert ( all ( size (param == [dims ] ) )
Sets the input parameter param to the size specified by dimensions dims. For example, to set the size of input U to a 3-by-2 matrix, call:
... assert(all(size(U)== [3 2])); ...
assert ( isscalar (param ) ) assert ( all ( size (param == [ 1 ] ) )
Sets the size of input parameter param to scalar. For example, to set the size of input U to scalar, call:
... assert(isscalar(U)); ...
or
... assert(all(size(U)== [1])); ...
assert ( isreal (param ) )
Specifies that the input parameter param is real. For example, to specify that input U is real, call:
... assert(isreal(U)); ...
assert ( ~isreal (param ) )
Specifies that the input parameter param is complex. For example, to specify that input U is complex, call:
... assert(~isreal(U)); ...
Specify numerictype of Fixed-Point Input.
assert ( isequal ( numerictype ( fiparam ), T ) )
Sets the numerictype properties of fi input parameter fiparam to the numerictype object T. For example, to specify the numerictype property of fixed-point input U as a signed numerictype object T with 32-bit word length and 30-bit fraction length, use the following code:
%#eml ... % Define the numerictype object. T = numerictype(1, 32, 30); % Set the numerictype property of input U to T. assert(isequal(numerictype(U),T)); ...
Specify fimath of Fixed-Point Input.
assert ( isequal ( fimath ( fiparam ), F ) )
Sets the fimath properties of fi input parameter fiparam to the fimath object F. For example, to specify the fimath property of fixed-point input U so that it saturates on integer overflow, use the following code:
%#eml
...
% Define the fimath object.
F = fimath('OverflowMode','saturate');
% Set the fimath property of input U to F.
assert(isequal(fimath(U),F));
... Specify Multiple Properties of Input.
assert ( function1 ( params ) &&
function2 ( params ) &&
function3 ( params ) && ... )
Specifies the class, size, and complexity of one or more inputs using a single assert function call. For example, the following code specifies that input U is a double, complex, 3-by-3 matrix, and input V is a 16-bit unsigned integer:
%#eml
...
assert(isa(U,'double') &&
~isreal(U) &&
all(size(U) == [3 3]) &&
isa(V,'uint16'));
... Follow these rules when using the assert function to specify the properties of primary function inputs:
Call assert functions at the beginning of the primary function, before any control-flow operations such as if statements or subroutine calls.
Do not call assert functions inside conditional constructs, such as if, for, while, and switch statements.
Use the assert function with Embedded MATLAB Coder only for specifying properties of primary function inputs before converting your M-code to C code.
If you set the class of an input parameter to fi, you must also set its numerictype and fimath properties (see Specify numerictype of Fixed-Point Input and Specify fimath of Fixed-Point Input).
If you set the class of an input parameter to struct, you must specify the class, size, and complexity of all fields in the order they appear in the structure definition.
In the following code excerpt, a primary MATLAB function emcspecgram takes two inputs: pennywhistle and win. The code specifies the following properties for these inputs:
| Input | Property | Value |
|---|---|---|
| pennywhistle | class | int16 |
| size | 220500-by-1 vector | |
| complexity | real (by default) | |
| win | class | double (by default) |
| size | 1024-by-1 vector | |
| complexity | real (by default) |
%#eml function y = emcspecgram(pennywhistle,win) nx = 220500; nfft = 1024; assert(isa(pennywhistle,'int16')); assert(all(size(pennywhistle) == [nx 1])); assert(isa(win, 'double')); assert(all(size(win) == [nfft 1])); ...
Alternatively, you can combine property specifications for one or more inputs inside assert commands, as follows:
%#eml function y = emcspecgram(pennywhistle,win) nx = 220500; nfft = 1024; assert(isa(pennywhistle,'int16') && all(size(pennywhistle) == [nx 1])); assert(isa(win, 'double') && all(size(win) == [nfft 1])); ...
In the following example, the primary MATLAB function emcsqrtfi takes one fixed-point input: x. The code specifies the following properties for this input:
| Property | Value |
|---|---|
| class | fi |
| numerictype | numerictype object T, as specified in the primary function |
| fimath | fimath object F, as specified in the primary function |
| size | scalar (by default) |
| complexity | real (by default) |
%#eml
function y = emcsqrtfi(x)
T = numerictype('WordLength',32,'FractionLength',23,
'Signed',true);
F = fimath('SumMode','SpecifyPrecision',
'SumWordLength',32,'SumFractionLength',23,
'ProductMode','SpecifyPrecision',
'ProductWordLength',32,'ProductFractionLength',23);
assert(isfi(x));
assert(isequal(numerictype(x),T));
assert(isequal(fimath(x),F));
y = sqrt(x);Assume you have defined S as the following scalar MATLAB structure:
S = struct('r',double(1),'i',int8(4));Here is code that specifies the class and size of S and its fields when passed as an input to your M-function:
%#eml function y = fcn(S) % Specify the class of the input as struct. assert(isstruct(S)); % Specify the class and size of the fields r and i % in the order in which you defined them. assert(isa(S.r,'double')); assert(isa(S.i,'int8'); ...
Note In most cases, Embedded MATLAB Coder uses defaults when you don't explicitly specify values for properties — except for structure fields. The only way to name a field in a structure is to set at least one of its properties. Therefore in the example above, an assert function specifies that field S.r is of type double, even though double is the default. |
For structure arrays, you must choose a representative element of the array for specifying the properties of each field. For example, assume you have defined S as the following 2-by-2 array of MATLAB structures:
S = struct('r',{double(1), double(2)},'i',{int8(4), int8(5)});The following code specifies the class and size of each field of structure input S using the first element of the array:
%#eml function y = fcn(S) % Specify the class of the input S as struct. assert(isstruct(S)); % Specify the size of the fields r and i % based on the first element of the array. assert(all(size(S) == [2 2])); assert(isa(S(1).r,'double')); assert(isa(S(1).i,'int8'));
Note In most cases, Embedded MATLAB Coder uses defaults when you don't explicitly specify values for properties — except for structure fields. The only way to name a field in a structure is to set at least one of its properties. Therefore in the example above, an assert function specifies that field S(1).r is of type double, even though double is the default. |
![]() | Configuring Your Environment for Code Generation | Choosing Your Target | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |