Prepare MATLAB Code for Code Generation
To generate C/C++ code, the code generator converts dynamically typed MATLAB® code to statically typed C/C++ code. In dynamically typed languages, the class, size, and complexity of a given variable can change at run time. In contrast, statically typed languages must determine variable types at compile-time.
Before generating code, identify which function to generate code for. This function is called the entry-point function or primary function. To prepare your code for code generation:
Initialize variables for code generation.
Screen your 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:
y = zeros(1,10); y(3) = 1 + 2i;
y is defined as double but assigned complex
y = complex(zeros(1,10)); y(3) = 1 + 2i;
for i = 1:N y(i,i) = i; end
y is extended dynamically without
y = zeros(N,N); for i = 1:N y(i,i) = i; end
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. See Functions and Objects Supported for C/C++ Code Generation. To check for unsupported functions and language features in your code:
Start the MATLAB Coder™ App from the Apps tab. Alternatively, enter this at the command line:
Enter the entry-point function name in the app. Do not add subfunctions in this step. The code generator automatically includes the required subfunctions.
To see the unsupported functions or language features in your code, open the Code Generation Readiness Tool Report by clicking Next.
Alternatively, call the
coder.screener function on your
entry-point function. At the command line,
This function parses your code and highlights unsupported MATLAB functions and some unsupported language features. See
If your code includes unsupported functions, consider these workarounds:
For more details of these workarounds, see Resolve Error: Function Is Not Supported for Code Generation.
Set Advanced Code Generation Options at Command Line
Research Code Generation Considerations for Specific Functions
The reference pages of functions supported for code generation contain a
section titled Extended Capabilities. This section lists
special considerations when generating code for those functions. For example,
see Extended Capabilities in
coder.extrinsic declare 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.