Main Content

Identify Entry-Point Functions and Check MATLAB Code

Before you generate code, you need to identify one or more entry-point functions. Then, use the Code Analyzer and the Code Generation Readiness Tool to check your MATLAB® code for common errors specific to code generation. Identifying entry-point functions and checking for common errors is the first step in the Code Generation Workflow.

Identify Entry-Point Functions

An entry-point function is a function that you want to access directly from your custom C/C++ code. The code generator preserves the signatures of functions identified as entry points. Entry-point functions are stable interfaces to the generated code. For functions that you do not designate as entry points, the code generator uses internal heuristics to balance code performance and readability. This means that the code generator can eliminate, inline, or modify non-entry-point function calls. Functions that you do not designate as entry points are not stable interfaces to the generated code.

The code generator supports several types of entry-point functions, including:

  • Single entry point, single generated function signature — For many applications, the entry point is a single top-level function that calls your other MATLAB functions. Use a single entry-point function when you want to interface with the generated code by using a single function signature.

  • Single entry point, multiple function signatures – In MATLAB, certain functions can accept a number of different data types as input. You can mimic this flexibility in the generated code by generating multiple signatures for a single entry-point function. See Generate Code for Functions with Multiple Signatures.

  • Multiple entry points, multiple function signatures – To generate stable interfaces for multiple functions in your MATLAB code, you can generate code for multiple entry points simultaneously. See Generate Code for Multiple Entry-Point Functions.

Use the Code Analyzer

To minimize code generation errors, use the Code Analyzer in the MATLAB Editor before you generate C/C++ code. The Code Analyzer checks your code as you enter it, reports problems, and recommends modifications.

To use the Code Analyzer to identify warnings and errors specific to code generation, add the %#codegen directive to your MATLAB files. For a list of the checks performed by the Code Analyzer when you add the %#codegen directive, see Performance Improvements.

If the indicator in the top right of the editor window is green, the Code Analyzer does not detect code generation issues.

Code analyzer window, showing green indicator

If the indicator is red, the Code Analyzer has detected errors in your code. If the indicator is orange, it has detected warnings. When the indicator is red or orange, a red or orange marker appears to the right of the code where the issue occurs. Point to the marker for information about the error or warning. Click the underlined text in the error message for a more detailed explanation and suggested actions.

Code analyzer window, showing red indicator and hyperlinks to error locations

Before generating code from your MATLAB code, fix the errors detected by the Code Analyzer.

Use the Code Generation Readiness Tool

The code generator supports most MATLAB 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, use the Code Generation Readiness Tool. To run the Code Generation Readiness Tool, use one of these methods:

  • In the MATLAB Coder™ app, load your entry-point function. The code generation tool readiness tool runs automatically.

  • In the browser, right-click the file that you want to check for code generation readiness and select Check Code Generation Readiness.

  • At the command line, use the coder.screener function.

The Code Generation Readiness Tool parses your code and attempts to identify unsupported MATLAB functions and language features. It is best practice to fix the issues listed by the tool before generating code. For more information, see Code Generation Readiness Tool.

Troubleshoot Errors Before Code Generation

The Code Analyzer and Code Generation Readiness Tools are unable to detect all possible code generation compliance issues in your MATLAB code. However, using these tools can help to minimize the number of troubleshooting and code generation cycles you must complete to generate working, optimized, and production-ready C/C++ or MEX code.

This table describes some of the code issues that are can be detected by the Code Analyzer or the Code Generation Readiness Tool and suggests some possible solutions.

Error MessageIssuePossible Solution
Code generation does not support scripts.You are trying to generate code for a MATLAB script.For code generation, your MATLAB code must be in a function file that contains only function definitions.
This function is not supported in code generation.Code generation does not support one or more of the MathWorks® functions used in your MATLAB code.

If your code includes unsupported functions, consider one of these workarounds:

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

  • Write custom MATLAB code to replace the unsupported functions.

  • Use coder.ceval to call custom C functions that replace the unsupported functions.

  • If you do not need to generate standalone code, use coder.extrinsic to call the unsupported functions.

For more details of these workarounds, see Resolve Error: Function Is Not Supported for Code Generation.

Code generation requires a variable to be fully defined through assignment before subscripting it.You are attempting to create or expand an array by assigning a value outside of existing index boundaries.In MATLAB, you can create or grow an array by assigning a value to an undefined array element. Code generation does not support this method of array creation and expansion. You must define a matrix before assigning values to its elements. See Define Matrices Before Assigning Indexed Variables.
Code generation only supports growing the size of an array through 'end + 1' indexing.You are attempting to grow an array by using end in a manner that code generation does not support.

To grow arrays using end in MATLAB code for code generation, you must adhere to certain restrictions:

  • Only use end to grow vectors.

  • Only grow vectors by assigning a value to the end+1 element. Code generation does not support assigning values to subsequent elements, such as end+2.

See Grow an Array Using (end + 1) Indexing.

Variable might be used before it is defined, this is not allowed in code generation.You have used a variable before it has been defined.Make sure all variables are defined before use. In many cases, this error indicates that this code will also not run in MATLAB.

See Also

Related Topics