Check for Issues in MATLAB Code Using MEX Functions - MATLAB & Simulink (original) (raw)

Main Content

Before generating C/C++ code or an accelerated MEX function, check for compliance issues in your MATLAB® code by generating and running a MEX function. This step is an important part of the Code Generation Workflow.

Checking for issues by using a MEX function enables you to:

Check for issues in your MATLAB code by using one of these methods:

Fix errors before generating standalone C/C++ code or an accelerated MEX function. Resolving errors that occur during the check-for-issues step typically requires revising your MATLAB code but can also require respecifying input types. If you change your MATLAB code, execute your revised code in the MATLAB environment to confirm that it is running correctly, then check for issues again. Completing the check-for-issues step without errors can require multiple iterations of generating and executing a MEX function.

Troubleshooting Errors During MEX Generation

If you use the MATLAB Coder app, the app produces an error report if the code generator detects errors or warnings during MEX generation. If you generate code at the command line, you can produce an error report by calling the codegen command with the -report or -launchreport option. The error report describes the identified issues and identifies the lines in your MATLAB code that have errors. See Code Generation Reports.

You can usually fix errors detected during MEX generation by modifying your MATLAB code. In some cases, you can fix errors by changing your code configuration settings or respecifying input types. This table describes some common errors that you can encounter during code generation and provides some suggested solutions.

Error Message Fragment Issue Possible Solution
Array element element is out-of-boundsCode generation does not support creating arrays via indexing The MATLAB code creates or expands 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.
Unable to make this assignment because dimension_dimension_ is fixed on the left side but variable size on the right The MATLAB code assigns a variable-size array to a variable that is defined as fixed size. Resolve this error by using one of these methods: Force the variables to be the same size by using explicit indexing.Allow the variable on the left size to vary by using coder.varsize.If the mismatch is due to the implicit expansion of the variable-size array, disable implicit expansionSee Resolve Error: Fixed Size on the Left Side but Variable Size on the Right.
Undefined function or variable 'name' The MATLAB code uses a function or variable that is unknown to the code generator. This code is also likely to fail in MATLAB execution. Make sure that your code runs in MATLAB before attempting to generate code.
Function 'name' not supported for code generationMethod not supported for code generation Code generation does not support one of the functions in the 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.
All variables must be fully defined before useAll cell array elements must be fully defined before use The code generator is unable to determine the types of one or more variables in the MATLAB code. Fully define all variables on all execution paths before use, including cell array elements, structure fields, and class properties. See Resolve Issue: Variables Must Be Fully Defined Before Use and Resolve Issue: Cell Array Elements Must Be Fully Defined Before Use.
Code generation does not support changing types through assignment The MATLAB code changes the properties of a variable by assigning it a value that has a different class or size. Assign each variable a fixed class. If you need to use a variable that changes in size, see Generate Code for Variable-Size Arrays. If you need to reuse variables in the generated code, see Reuse the Same Variable with Different Properties.
Arrays have incompatible sizesUnable to perform this operation because the sizes of the arrays are incompatible The code generator can produce array size incompatibility errors in either of these situations: The MATLAB code changes the size of a variable that the code generator previously defined as fixed size.The MATLAB code performs an operation on two arrays with incompatible sizes. To resolve this error, try one these approaches: Instruct the code generator to allow the variable to change size by usingcoder.varsizePerform binary operations on array of compatible sizes.See Resolve Error: Arrays Have Incompatible Sizes.

See also MATLAB Code Design Considerations for Code Generation and MATLAB Language Features Supported for C/C++ Code Generation.

If your MATLAB code calls functions on the MATLAB path, the code generator attempts to generate code for each function unless the function is extrinsic. See Resolution of Function Calls for Code Generation.

Troubleshooting Errors During MEX Execution

When you run a generated MEX function in MATLAB, memory integrity checks are executed by default. These checks perform array bounds and dimension checking, and detect violations of memory integrity in code generated for MATLAB functions. Generated MEX functions also perform responsiveness checks by default, which allow you to terminate MEX execution withCtrl+C.

The most common causes of the errors that occur when you run a MEX function are:

To learn more about best practices when debugging MEX functions, see Debugging Generated MEX Code.

See Also

codegen | coder.runTest

Topics