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 using a C/C++ debugger, set the EnableDebugging property of the MEX configuration object to true, and then generate C/C++ code with the codegen command. For example, if foo is the name of your MATLAB function, you can configure and generate the code using these commands:

cfg = coder.config('mex');
cfg.EnableDebugging = true;
codegen -config cfg foo
Alternatively, you can generate the MEX file using the debug option -g in the codegen command.

codegen -g foo

The foo_mex file gets generated in the current working directory.

To debug the generated MEX using a C/C++ debugger on a: