Main Content

Prepare MATLAB Code for Code Generation

To generate C/C++ code, the code generator converts dynamically typed MATLAB® code to statically typed C/C++. Dynamically typed MATLAB variables can change their properties at run time. The same variable can hold a value of any class, size, or complexity. Statically typed languages such as C/C++ must determine variable types at compile time.

Before generating code, identify which function to generate code for. This function is termed the entry-point function or primary function. To prepare your code for code generation:

  1. Initialize variables for code generation.

  2. Screen code for unsupported functions and language features.

Initialize Variables for Code Generation

Because the generated code is statically typed, initialize all variables in your code before use to allow the code generator to identify and allocate the variables properly in the generated code. To identify some of these issues, include this line in your code.

This table lists some common errors that might occur while initializing variables in code intended for code generation.

Original CodeIssueModified Code
y = zeros(1,10);
y(3) = 1 + 2i;
y is defined as double but assigned complex double value.
y = complex(zeros(1,10));
y(3) = 1 + 2i;
for i = 1:N
    y(i,i) = i;
The array y is extended dynamically without being defined.
y = zeros(N,N);
for i = 1:N
    y(i,i) = i;

For information about data definition for code generation of specific data types, see Data Definition Considerations for Code Generation and Best Practices for Defining Variables for C/C++ Code Generation.

Screen Code for Unsupported Functions and Language Features

The code generator supports most language features and functions. To check for unsupported functions and language features in your code:

  1. Start the MATLAB Coder™ App from the APPS tab. Alternatively, type this in the command line:

    >> coder

  2. Enter the entry-point function name in the app. Do not add sub-functions in this step. The code generator automatically includes all required sub-functions.

  3. To obtain the Code Generation Readiness Tool Report, click Next. If there are unsupported functions or language features in your code, they are reported here.

Alternatively, call the screener on your entry-point function. At the command line, run this command:


This tool parses your code and highlights unsupported MATLAB functions and some unsupported language features. See Functions and Objects Supported for C/C++ Code Generation & coder.screener.

If your code includes unsupported functions, consider these workarounds:

  • Check for replacement functions and System objects that support code generation.

  • Write custom code for those functions.

    Use coder.ceval to call a custom C function you have for that function.

  • Use coder.extrinsic to call the function.


Set Advanced Code Generation Options at the Command Line

Use the codegen function with the configuration object coder.config. Depending on the type of build, you can also use coder.CodeConfig, coder.EmbeddedCodeConfig, and coder.MexCodeConfig.

Research Code Generation Considerations for Specific Functions?

For functions supported for code generation, their reference pages contain a section titled Extended Capabilities. This section lists all special considerations when generating code for those functions. For example, see Extended Capabilities in interp2.

The coder.extrinsic Call

Calls to coder.extrinsic declares a function as an extrinsic function. The code generator does not produce code for the body of the extrinsic function and instead uses the MATLAB® engine to execute the call.

See Also

| | | | | | | |

Related Topics