This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Debug on Microsoft Windows Platforms

This example shows how to debug yprime.c, found in your matlabroot/extern/examples/mex/ folder, with Microsoft® Visual Studio® 2012. If you are using a different version, refer to the corresponding Microsoft documentation.

  1. Make sure Visual Studio is your selected C compiler:

    cc = mex.getCompilerConfigurations('C','Selected');
    ans =
    Microsoft Visual C++ 2012 (C)
  2. Compile the source MEX file with the -g option, which builds the file with debugging symbols included. For example:

    mex -g yprime.c

  3. Start Visual Studio. Do not exit your MATLAB® session.

  4. From the Visual Studio Tools menu, select Attach to Process

  5. In the Attach to Process dialog box, select the MATLAB process, and click Attach.

    Visual Studio loads data then displays an empty code pane.

  6. Open the source file yprime.c by selecting File > Open > File. Locate yprime.c in the folder, c:\work.

  7. Set a breakpoint by right-clicking the desired line of code and following Breakpoint > Insert Breakpoint on the context menu. It is often convenient to set a breakpoint at mexFunction to stop at the beginning of the gateway routine.

    If you have not yet run the executable file, ignore any “!” icon that appears with the breakpoint next to the line of code.

    Once you hit one of your breakpoints, you can make full use of any commands the debugger provides to examine variables, display memory, or inspect registers.

  8. Open MATLAB and type:


    yprime.c is opened in the Visual Studio debugger at the first breakpoint.

  9. If you select Debug > Continue, MATLAB displays:

    ans =
        2.0000    8.9685    4.0000   -1.0947

    For more information on how to debug in the Visual Studio environment, see your Microsoft documentation.

Notes on Debugging

Binary MEX files built with the -g option do not execute on other computers because they rely on files that are not distributed with MATLAB. For more information on isolating problems with MEX files, see Troubleshoot MEX Files.

Do not enable "Access Violation" for handling exceptions when debugging MEX files. To avoid breaking at this exception, clear the check box for Win32 Exceptions. For Visual Studio 2015, look for the check box in Debug > Windows > Exception Settings.... If you are using a different version of Visual Studio, refer to the corresponding Microsoft documentation.

Related Topics

External Websites