13

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.

4
  • 2
    Side note: matlab -nodesktop -nojvm is even faster! Commented Apr 4, 2014 at 3:54
  • 1
    -nojvm does not allow me to plot graphs. Commented Apr 4, 2014 at 4:05
  • ah I see. Yeah it has its limitations. Good for running quick-and-dirty code. Commented Apr 4, 2014 at 4:06
  • 1
    Maybe consider an editor which has an matlab mode - like emacs or vim. It might be easier to set breakpoints from there and step through. Commented Apr 4, 2014 at 8:24

4 Answers 4

20

To set breakpoints with the command line, dbstop is the tool (plus dbclear to clear breakpoints and dbstatus to list them).

There are presently 17 different forms to dbstop, which allow you to specify various combinations of:

  1. The M-file in which to stop
  2. Line number
  3. Sub-function
  4. Conditional to an arbitrary expression. For example,

    dbstop in myFun.m at 224 if ~exist('x','var')
    
  5. At any run-time error (dbstop if error)
  6. At a specific error (e.g dbstop if error myFun.m:barErrorId)
  7. At any warning (dbstop if warning) or specific warning
  8. If NaN or Inf 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 (also dbstep 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 and dbdown, 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 is checkcode, 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 a keyboard into your code, but it's always an option.

Sign up to request clarification or add additional context in comments.

Comments

7

Try placing the keyboard command in your code to insert a breakpoint. When the keyboard command is reached, MATLAB will drop into an interactive prompt that you can use to inspect variables. For example:

x = rand(10,10);
y = rand(10,5);
z = x * y;
keyboard; % you can interactively inspect x, y, z here
z = sort(z);

To leave keyboard mode, you can type dbquit to exit the program, or return 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.

1 Comment

That was what I was looking for. Are there any more tricks like interactively enabling and disabling a breakpoint?
1

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.

2 Comments

I dont understand what you mean here. Using that will allow me to debug matlab as an executable , but not a .m script itself. Or am I missing something here ?
My mistake. I uninstalled MATLAB from my system a while ago, so couldn't validate my own answer. Let me edit my answer :)
0

You can use MATLAB's editor debug button to debug within MATLAB environment

1 Comment

The OP is asking about the command line without the GIO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.