Main Content

Running and Debugging MEX Functions

When you call a 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.

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

Debug MEX Functions

To debug your MEX functions, 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 Data Read and Write Considerations

Debug MEX Functions by Using a C/C++ Debugger

To debug your MEX functions by using a C/C++ debugger, set the MEX configuration object property EnableDebugging to true:

cfg = coder.config('mex');
cfg.EnableDebugging = true;
codegen -config cfg foo_mex
Alternatively, you can debug your MEX function by executing this command:

codegen -g foo_mex

The foo_mex file is the MEX file that you intend to debug. You can debug this file by using a C or C++ debugger. For more information on debugging by using a C/C++ debugger on a Microsoft Windows platform, see Debug on Microsoft Windows Platforms.

For more information on debugging by using a C/C++ debugger on a Linux® or Mac platform, see Debug on Linux Platforms or Debug on macOS Platforms.