Why Test MEX Functions in MATLAB?

Before generating C/C++ code for your MATLAB® code, it is a best practice to test the MEX function to verify that it provides the same functionality as the original MATLAB code. To do this testing, run the MEX function using the same inputs as you used to run the original MATLAB code and compare the results. For more information about how to test a MEX function using the MATLAB Coder™ app, see Check for Run-Time Issues by Using the App and Verify MEX Functions in the MATLAB Coder App. For more information about how to test a MEX function at the command line, see Verify MEX Functions at the Command Line.

Running the MEX function in MATLAB before generating code enables you to detect and fix run-time errors that are much harder to diagnose in the generated code. If you encounter run-time errors in your MATLAB functions, fix them before generating code. See Fix Errors Detected at Code Generation Time and Debug Run-Time Errors.

When you run your MEX function in MATLAB, by default, the following run-time checks execute:

  • Memory integrity checks. These checks perform array bounds checking, dimension checking, and detect violations of memory integrity in code generated for MATLAB functions. If a violation is detected, MATLAB stops execution and provides a diagnostic message.

  • Responsiveness checks in code generated for MATLAB functions. These checks enable periodic checks for Ctrl+C breaks in code generated for MATLAB functions, allowing you to terminate execution with Ctrl+C.

For more information, see Control Run-Time Checks.