coder.opaque
Declare variable in generated code
Syntax
Description
y = coder.opaque(
declares
a variable type
)y
with the specified type and no
initial value in the generated code.
y
can be a variable or a structure field.MATLAB® code cannot set or access
y
, but external C functions can accepty
as an argument.y
can be an:Argument to
coder.rref
,coder.wref
, orcoder.ref
Input or output argument to
coder.ceval
Input or output argument to a user-written MATLAB function
Input to a subset of MATLAB toolbox functions supported for code generation
Assignment from
y
declares another variable with the same type in the generated code. For example:declares a variabley = coder.opaque('int'); z = y;
z
of typeint
in the generated code.You can assign
y
from another variable declared using eithercoder.opaque
or assignment from a variable declared usingcoder.opaque
. The variables must have identical types.You can compare
y
to another variable declared using eithercoder.opaque
or assignment from a variable declared usingcoder.opaque
. The variables must have identical types.
y = coder.opaque(___,'Size',
specifies
the size, in bytes, of Size
)y
. You can specify the size
with any of the previous syntaxes.
y = coder.opaque(___,'HeaderFile',
specifies
the header file that contains the type definition. The code generator
produces the HeaderFile
)#include
statement for the header
file where the statement is required in the generated code. You can
specify the header file with any of the previous syntaxes.
Examples
Input Arguments
Tips
Specify a
value
that has the type thattype
specifies. Otherwise, the generated code can produce unexpected results. For example, the followingcoder.opaque
declaration can produce unexpected results.y = coder.opaque('int', '0.2')
coder.opaque
declares the type of a variable. It does not instantiate the variable. You can instantiate a variable by using it later in the MATLAB code. In the following example, assignment offp1
fromcoder.ceval
instantiatesfp1
.% Declare fp1 of type FILE * fp1 = coder.opaque('FILE *'); %Create the variable fp1 fp1 = coder.ceval('fopen', ['testfile.txt', char(0)], ['r', char(0)]);
In the MATLAB environment,
coder.opaque
returns the value specified invalue
. Ifvalue
is not provided, it returns an empty character vector.You can compare variables declared using either
coder.opaque
or assignment from a variable declared usingcoder.opaque
. The variables must have identical types. The following example demonstrates how to compare these variables. Compare Variables Declared Using coder.opaqueTo avoid multiple inclusions of the same header file in generated code, enclose the header file in the conditional preprocessor statements
#ifndef
and#endif
. For example:#ifndef MyHeader_h #define MyHeader_h <body of header file> #endif
You can use the MATLAB
cast
function to cast a variable to or from a variable that is declared usingcoder.opaque
. Usecast
withcoder.opaque
only for numeric types.To cast a variable declared by
coder.opaque
to a MATLAB type, you can use theB = cast(A,type)
syntax. For example:x = coder.opaque('size_t','0'); x1 = cast(x, 'int32');
You can also use the
B = cast(A,'like',p)
syntax. For example:x = coder.opaque('size_t','0'); x1 = cast(x, 'like', int32(0));
To cast a MATLAB variable to the type of a variable declared by
coder.opaque
, you must use theB = cast(A,'like',p)
syntax. For example:x = int32(12); x1 = coder.opaque('size_t', '0'); x2 = cast(x, 'like', x1));
Use
cast
withcoder.opaque
to generate the correct data types for:Inputs to C/C++ functions that you call using
coder.ceval
.Variables that you assign to outputs from C/C++ functions that you call using
coder.ceval
.
Without this casting, it is possible to receive compiler warnings during code generation.
Extended Capabilities
Version History
Introduced in R2011a