Main Content

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.

If you generate code by using the codegen command, you use the -args option to specify the input types. If you generate code by using the MATLAB Coder™ app, you specify the input types in the Define Input Types page.

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 and b as real double scalars. To generate code for these inputs, run these commands:

    a = 1;
    codegen -config:lib myMultiply -args {a,a}
    The generated C source file myMultiply.c contains the C function:

    double myMultiply(double a, double b)
    {
      return a * b;
    }
  • Specify a and b as real double 5-by-5 matrices. To generate code for these inputs, run these commands:

    a = zeros(5,5);
    codegen -config:lib myMultiply -args {a,a}
    The generated C source file 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] and const double b[25] correspond to the inputs a and b in the MATLAB code. The size of the one-dimensional arrays a and b in the C code is 25, which is equal to the total number of elements in example input arrays that you used when you called the codegen function.

    The C function has one more argument: the one-dimensional array y of size 25. 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 the coder.typeof function. coder.typeof(A,B,1) specifies a variable-size input with the same class and complexity as A and upper bounds given by the corresponding element of the size vector B.

    Specify a and b as real double arrays that are of variable-size, with a maximum size of 10 on either dimension. To generate code, run these commands:

    a = coder.typeof(1,[10 10],1);
    codegen -config:lib myMultiply -args {a,a}
    The signature of the generated C function is:

    void 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])
    The arguments a_data, b_data, and y_data correspond to the input arguments x and b and the output argument y in the original MATLAB function. The C function now accepts three additional arguments, a_size, b_size, and y_size, that specify the sizes of a_data, b_data, and y_data at run time.

See Also

|

Related Topics