Input Type Specification for Code Generation
C/C++ and MATLAB® handle variables differently. Some of these differences that affect the code generation workflow are:
C/C++ source code includes type declarations for all variables. The C/C++ compiler uses these declarations to determine the types of all variables at compile time. MATLAB code does not include explicit type declarations. The MATLAB execution engine determines the types of variables at run time.
In C/C++, the memory for arrays can be either statically declared at compile time (fixed size arrays), or dynamically allocated at run time (variable-size arrays). All MATLAB arrays use dynamically allocated memory and are of variable size.
To allow the generation of C/C++ code with specific types, you must specify the properties (class, size, and complexity) of all input variables to the MATLAB entry-point functions during C/C++ or MEX code generation. An entry-point function is a top-level MATLAB function from which you generate code. The code generator uses these input properties to determine the properties of all variables in the generated code. Different input type specifications can cause the same MATLAB code to produce different versions of the generated code.
You can specify input types:
To see how input type specification affects the
generated code, consider a simple MATLAB function myMultiply
that multiplies two quantities
a
and b
and returns the value of the
product.
function y = myMultiply(a,b) y = a*b; end
Generate static C library code for three different type specifications for the input
arguments a
and b
. In each case, inspect the
generated code.
Specify
a
andb
as real double scalars. To generate code for these inputs, run these commands:The generated C source filea = 1; codegen -config:lib myMultiply -args {a,a}
myMultiply.c
contains the C function:double myMultiply(double a, double b) { return a * b; }
Specify
a
andb
as real double5
-by-5
matrices. To generate code for these inputs, run these commands:The generated C source filea = zeros(5,5); codegen -config:lib myMultiply -args {a,a}
myMultiply.c
contains the C function:void myMultiply(const double a[25], const double b[25], double y[25]) { int i; int i1; double d; int i2; for (i = 0; i < 5; i++) { for (i1 = 0; i1 < 5; i1++) { d = 0.0; for (i2 = 0; i2 < 5; i2++) { d += a[i + 5 * i2] * b[i2 + 5 * i1]; } y[i + 5 * i1] = d; } } }
const double a[25]
andconst double b[25]
correspond to the inputsa
andb
in the MATLAB code. The size of the one-dimensional arraysa
andb
in the C code is25
, which is equal to the total number of elements in example input arrays that you used when you called thecodegen
function.The C function has one more argument: the one-dimensional array
y
of size25
. It uses this array to return the output of the function.You can also generate code that has the same array dimensions as the MATLAB code. See Generate Code That Uses N-Dimensional Indexing.
Finally, you generate code for
myMultiply
that can accept input arrays of many different sizes. To specify variable-size inputs, you can use thecoder.typeof
function.coder.typeof(A,B,1)
specifies a variable-size input with the same class and complexity asA
and upper bounds given by the corresponding element of the size vectorB
.Specify
a
andb
as real double arrays that are of variable-size, with a maximum size of10
on either dimension. To generate code, run these commands:The signature of the generated C function is:a = coder.typeof(1,[10 10],1); codegen -config:lib myMultiply -args {a,a}
The argumentsvoid myMultiply(const double a_data[], const int a_size[2], const double b_data[], const int b_size[2], double y_data[], int y_size[2])
a_data
,b_data
, andy_data
correspond to the input argumentsa
andb
and the output argumenty
in the original MATLAB function. The C function now accepts three additional arguments,a_size
,b_size
, andy_size
, that specify the sizes ofa_data
,b_data
, andy_data
at run time.