| MATLAB® | ![]() |
| On this page… |
|---|
The examples show how to debug timestwo.F, found in your matlabroot/extern/examples/refbook/ 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 MEX-Files for additional information on isolating problems with MEX-files.
For MEX-files compiled with any version of the Intel Visual Fortran compiler, you can use the debugging tools found in your version of Microsoft Visual Studio. Refer to the "Creating C Language MEX-Files" topic Debugging on the Microsoft Windows Platforms for instructions on using this debugger.
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.
The MATLAB supported Fortran compiler g95 has a -g option for building binary MEX-files with debug information. Such files can be used with gdb, the GNU Debugger. This section describes using gdb.
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 example, the MATLAB command prompt >> is shown in front of MATLAB commands, and linux> represents a Linux[1] prompt; your system may show a different prompt. The debugger prompt is <gdb>.
To compile the source MEX-file, type:
linux> mex -g timestwo.F
On a Linux 32–bit platform, this command creates the executable file timestwo.mexglx.
At the Linux prompt, start the gdb debugger using the matlab -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 >> y = timestwo(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.
Note The function name may be slightly altered by the compiler (e.g., it may have an underscore appended). To determine how this symbol appears in a given MEX-file, use the Linux command nm. For example: linux> nm timestwo.mexglx | grep -i mexfunction The operating system responds with something like: 0000091c T mexfunction_ Use mexfunction_ in the breakpoint statement. Be sure to use the correct case. |
<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 continue:
<gdb> continue
After stopping at the last breakpoint, type:
<gdb> continue
timestwo finishes and MATLAB displays:
y =
8From 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] Linux is a registered trademark of Linus Torvalds.
![]() | Advanced Topics | Calling MATLAB Software from C and Fortran Programs | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |