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:
Initialize variables for code generation.
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.
|Original Code||Issue||Modified Code|
y = zeros(1,10); y(3) = 1 + 2i;
y = complex(zeros(1,10)); y(3) = 1 + 2i;
for i = 1:N y(i,i) = i; end
|The array ||
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. To check for unsupported functions and language features in your code:
Start the MATLAB Coder™ App from the APPS tab. Alternatively, type this in the command line:
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.
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 &
If your code includes unsupported functions, consider these workarounds:
Set Advanced Code Generation Options at the Command Line
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
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.