I have recently started using MATLAB without GUI by starting matlab with -nodesktop option and it is considerably faster.
However presently I have no way to debug a .m script in non gui mode. I have to open the default matlab editor every time I have to debug.Has anyone figured out a way to do it? Thanks in advance
I am using Ubuntu Linux, in case that helps.
Try placing the
keyboard
command in your code to insert a breakpoint. When thekeyboard
command is reached, MATLAB will drop into an interactive prompt that you can use to inspect variables. For example:To leave keyboard mode, you can type
dbquit
to exit the program, orreturn
to continue executing the program.Another trick is to turn on
dbstop if error
which will automatically drop you into an interactive prompt whenever your code crashes.You can use
MATLAB -Dgdb
if that helps. This sets gdb as the debugger. You will need to be familiar with gdb of course.Once you do that, use the standard gdb commands to debug it.
EDIT
My mistake. Above won't work for M-Files. (Not having MATLAB to try things out is a pain :)
MATLAB has a pretty good set of debugging commands you can use from the commandline. If you insert keyboard commands in your MATLAB code, you can then use the commands.
To set breakpoints with the command line,
dbstop
is the tool (plusdbclear
to clear breakpoints anddbstatus
to list them).There are presently 17 different forms to
dbstop
, which allow you to specify various combinations of:Conditional to an arbitrary expression. For example,
dbstop if error
)dbstop if error myFun.m:barErrorId
)dbstop if warning
) or specific warningNaN
orInf
are encountered (dbstop if naninf
)See the documentation for
dbstop
for details and good examples.Also get used to
dbcont
(or F5),dbstep
(or F10),dbquit
(Shift+F5),dbstep
(alsodbstep in
,dbstep out
),dbstack
(to see where you are and how you got there). The keyboard shortcuts may be different outside of Windows.Far less used, but still very useful are
dbup
anddbdown
, which allow you to switch workspace context (memory stacks).See the summary of functions and a list of examples and how-to pages in the MathWorks page on Debugging.
Related to the "
db
" functions ischeckcode
, which will check your code for possible problems before you even run it. This is a nice substitute for the red squiggly underlines that you would get in the MATLAB Editor.Once you get a hang of
dbstop
and it's syntax, you won't often need to insert akeyboard
into your code, but it's always an option.You can use MATLAB's editor debug button to debug within MATLAB environment