The Microsoft® Visual Studio® development environment provides complete source code debugging, including the ability to set breakpoints, examine variables, and step through the source code line-by-line.
For information on debugging MEX-files compiled with other MATLAB supported compilers, see Debugging on Windows in Technical Note 1605, MEX-files Guide.
Visual Studio 2005
This section describes how to debug using the default compiler, that is, the compiler used to build MATLAB.
-
Select the Microsoft Visual C++ 2005 compiler. At the MATLAB prompt, type:
mex -setup
Type y to locate installed compilers, and then type the number corresponding to this compiler.
-
Next, compile the source MEX-file with the -g option, which builds the file with debugging symbols included. For example:
mex -g yprime.c
On a 32–bit platform, this command creates the executable file yprime.mexw32.
-
From the Visual Studio Tools menu, select Attach to Process...[]
-
In the Attach to Process dialog box, select the MATLAB process and click Attach.
Visual Studio loads data then displays an empty code pane.
-
Open the source file yprime.c by selecting File > Open > File. yprime.c is found in the matlabroot/extern/examples/mex/ folder.
-
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 atmexFunction 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.
-
Run the binary MEX-file in MATLAB. After typing:
yprime(1,1:4)
yprime.c is opened in the Visual Studio debugger at the first breakpoint.
-
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.