Main Content

Check for Issues in MATLAB Code Using MEX Functions

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:

  • Detect and fix run-time errors that are more difficult to diagnose in the generated code. The run-time error checks that are enabled by default for MEX generation are disabled for standalone code generation.

  • Verify that the generated code provides the same functionality as your original MATLAB function. If you write a script to test your MATLAB code (also known as a test bench), you can use the same test bench script to test the generated MEX function.

  • Complete the error identification and code generation cycle more quickly. The MATLAB Coder™ app uses just-in-time (JIT) compilation when checking for issues, reducing code generation time. You can also enable JIT compilation when checking for issues at the command line.

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

  • In the MATLAB Coder app, on the code generation toolstrip, click the Run Generated MEX button. You must provide a test bench file that exercises your entry points.

  • At the command line, generate a MEX function by using the codegen command. Then, run the generated MEX function in MATLAB using the same inputs that you used to run your original MATLAB code. Alternatively, if you write a test bench script, you can test the generated MEX using coder.runTest.

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 FragmentIssuePossible Solution

Array element element is out-of-bounds

Code 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 rightThe 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 expansion

See 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 generation

Method 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 use

All 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 assignmentThe 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 sizes

Unable 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 using coder.varsize

  • Perform 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 with Ctrl+C.

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

  • Unsound input values — The inputs that you supply to the MEX function are incompatible with the inputs that the generated code expects. In many cases, these inputs also cause the MATLAB function to fail in MATLAB. Supply the same inputs to the generated MEX function that you use to run your MATLAB function.

  • Run-time type or size mismatch — The inputs that you supply to the MEX function at run time are inconsistent with the input types that you specified during code generation. To resolve this issue, use the code generation report to compare the variable definitions in the generated code to the variables that you passed at run time. Revise your MATLAB entry-point functions to accommodate the inputs that you want to pass at run time, and respecify input types accordingly. See Specify Types of Entry-Point Function Inputs.

  • Variable-size data — One or more variables in the generated code changes size during MEX execution, and certain assumptions made by the code generator about the size of those variables are falsified by the run-time inputs. To learn about techniques you can use to resolve this type of error, see Generate Code for Variable-Size Arrays.

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

See Also

|

Topics