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 Check for Issues page, call your entry-point function using the same inputs that you used to run your original MATLAB code. Alternatively, if you write a test bench script, you can browse for and select the script file.
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 usingcoder.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 Fragment | Issue | Possible Solution |
---|---|---|
| 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. |
Dimension
| 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:
|
Undefined
function or variable
' | 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. |
| Code generation does not support one of the functions in the MATLAB code. | If your code includes unsupported functions, consider one of these workarounds:
For more details of these workarounds, see Resolve Error: Function Is Not Supported for Code Generation. |
| 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 Code Generation for Variable-Size Arrays. If you need to reuse variables in the generated code, see Reuse the Same Variable with Different Properties. |
Size mismatch (size
| The MATLAB code assigns a value with dimensions
RHS_array_dimensions to a variable
with dimensions LHS_array_dimensions .
These dimensions are incompatible and/or you have disabled implicit
expansion. | Two arrays are compatible if each pair of dimensions is the
same size. If you enable implicit expansion, arrays are also
compatible if the size of at least one of the paired dimensions
is
|
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 Code Generation for Variable-Size Arrays.