| MATLAB® | ![]() |
| On this page… |
|---|
The examples show how to debug yprime.c, found in your matlabroot/extern/examples/mex/ directory.
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® software. Refer to the "Calling C and Fortran Programs from MATLAB" topic Troubleshooting for additional information on isolating problems with MEX-files.
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 Technical Note 1605, MEX-files Guide, at http://www.mathworks.com/support/tech-notes/1600/1605.html.
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...[1]

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/ directory.

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.

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.
The GNU Debugger gdb, available on Linux®[2] systems, 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 Technical Note 1605, MEX-files Guide, at http://www.mathworks.com/support/tech-notes/1600/1605.html.
In this procedure, the MATLAB command prompt >> is shown in front of MATLAB commands, and linux> represents a Linux prompt; your system may show a different prompt. The debugger prompt is <gdb>.
To debug with gdb:
Compile the source MEX-file with the -g option, which builds the file with debugging symbols included. For this example, at the Linux prompt, type:
linux> mex -g yprime.c
On a Linux 32–bit platform, this command creates the executable file yprime.mexglx.
At the Linux prompt, start the gdb debugger using the matlab function -D option:
linux> matlab -Dgdb
Start MATLAB without the Java™ Virtual Machine (JVM™) by using the -nojvm startup flag:
<gdb> run -nojvm
In MATLAB, enable debugging with the dbmex function and run your binary MEX-file:
>> dbmex on >> yprime(1,1:4)
At this point, you are ready to start debugging.
It is often convenient to set a breakpoint at mexFunction so you stop at the beginning of the gateway routine.
<gdb> break mexFunction <gdb> continue
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.
To proceed from a breakpoint, type:
<gdb> continue
After stopping at the last breakpoint, type:
<gdb> continue
yprime finishes and MATLAB displays:
ans =
2.0000 8.9685 4.0000 -1.0947
From the MATLAB prompt you can return control to the debugger by typing:
>> dbmex stop
Or, if you are finished running MATLAB, type:
>> quit
When you are finished with the debugger, type:
<gdb> quit
You return to the Linux prompt.
Refer to the documentation provided with your debugger for more information on its use.
[1] used by permission
[2] Linux is a registered trademark of Linus Torvalds.
![]() | Advanced Topics | Creating Fortran MEX-Files | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |