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 use these classes with only 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™, to run the MEX function, you must have a Computer Vision Toolbox license.

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

Debug MEX Functions

To debug your MEX functions, use the disp function to inspect the contents of your MEX function variables. You cannot use save to debug MEX function variables because code generation does not support it. Code generation does not support declaration of save as extrinsic. You can also use the fprintf function to inspect the contents of your MEX function variables.

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 1.

cfg = coder.config('mex');
cfg.EnableDebugging = 1;
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 Mac Platforms.