Main Content

Debugging Generated MEX Code

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. To learn how to troubleshoot common errors, see Check for Issues in MATLAB Code Using MEX Functions.

In addition, follow these best practices to debug MEX functions:

  • When you call the MEX function, pass it the same inputs that you use for the original MATLAB algorithm. Do not pass coder.Constant or any of the coder.Type classes to a MEX function. You can only use these classes with the codegen function.

  • To run a MEX function generated by MATLAB Coder™, you must have licenses for all the toolboxes that the MEX function requires. For example, if you generate a MEX function from a MATLAB algorithm that uses a Computer Vision Toolbox™ function or System object™, you must have a Computer Vision Toolbox license to run the MEX function.

  • Review the Extended Capabilities section in the reference pages for the functions you include in your code. The behavior of some MATLAB functions differ in the generated code. See Differences Between Generated Code and MATLAB Code.

  • Review calls to external C/C++ functions that use coder.ceval. Verify that the data type, input, and output layout are correct. MATLAB uses a column-major layout by default.

  • After you upgrade MATLAB, regenerate MEX functions before running them with the new version.

  • Use the disp or fprintf functions to inspect the contents of your MEX function variables. Alternatively, you can use the save function to save MEX function variables to a file in MAT or ASCII format. See Perform Data Read and Write Operations in Generated Code

  • Debug MEX functions by using a C/C++ debugger. Enable C/C++ debug mode by using the Enable C/C++ compiler debug mode parameter before you generate code. Alternatively, generate a MEX function in debug mode at the command line by using the codegen command with the -g option.

See Also

|

Topics