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.
ycan be a variable or a structure field.MATLAB® code cannot set or access
y, but external C functions can acceptyas an argument.ycan be an:Argument to
coder.rref,coder.wref, orcoder.refInput or output argument to
coder.cevalInput or output argument to a user-written MATLAB function
Input to a subset of MATLAB toolbox functions supported for code generation
Assignment from
ydeclares another variable with the same type in the generated code. For example:declares a variabley = coder.opaque('int'); z = y;zof typeintin the generated code.You can assign
yfrom another variable declared using eithercoder.opaqueor assignment from a variable declared usingcoder.opaque. The variables must have identical types.You can compare
yto another variable declared using eithercoder.opaqueor 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
valuethat has the type thattypespecifies. Otherwise, the generated code can produce unexpected results. For example, the followingcoder.opaquedeclaration can produce unexpected results.y = coder.opaque('int', '0.2')coder.opaquedeclares 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 offp1fromcoder.cevalinstantiatesfp1.% 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.opaquereturns the value specified invalue. Ifvalueis not provided, it returns an empty character vector.You can compare variables declared using either
coder.opaqueor 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
#ifndefand#endif. For example:#ifndef MyHeader_h #define MyHeader_h <body of header file> #endif
You can use the MATLAB
castfunction to cast a variable to or from a variable that is declared usingcoder.opaque. Usecastwithcoder.opaqueonly for numeric types.To cast a variable declared by
coder.opaqueto 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
castwithcoder.opaqueto 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