Community Highlights

Poll is CLOSED

Poll

What is your main tool or approach for debugging MATLAB code?

Set breakpoints
60%
Run with "Pause On Errors"
11%
The dbstop() command
2%
Pause the code with keyboard()
3%
Get help from Matlab Community
16%
Other
7%
2506 votes

Michaela Maier
Michaela Maier on 27 Jul 2022 at 18:17
i don't know why, but i often have problems that no amount of 'clear all' will solve (often involving MVMEvent/invokeListener) but restarting matlab will.
David Cazenave
David Cazenave on 16 Jul 2022 at 0:11
I go to documentation and answers.
Sheridan McPheeters
Sheridan McPheeters on 21 Jun 2022
Now that I know that "pause on error" exists, I'll probably use it instead of breakpoints... will have to look into these other options, as well. If they were around when I first started learning MATLAB in 2008 or so, I wasn't instructed on them. I guess that breakpoints are also a nearly universal debugging tool, so it's easy to get caught in the habit of using them (even though I code almost exclusively in MATLAB, these days).
Michael Van de Graaff
Michael Van de Graaff on 25 Apr 2022
Learning about the keyboard command was life changing for me
Matt J
Matt J on 27 Apr 2022
But why not dbstop, or inserted break points?
Michael Van de Graaff
Michael Van de Graaff on 21 May 2022
I think i learned about keyboard before conditional dbstop. I just put problem code in try blocks and then keyboard in the catch block.
Lukas Öhlund
Lukas Öhlund on 20 Apr 2022

disp('Got this far no29') works pretty well ;)

Joking aside, "Pause on Errors" has worked best for me - when it works. 9/10 times it does not trigger on an error for me. How do you guys get it to work?

Christian Stadelmann
Christian Stadelmann on 20 Apr 2022

It only works if the error is not being caught by some try/catch block. To also pause on caught errors, follow these steps (Matlab 2016b): 1. in the "Editor" tab, open the menu below "Breakpoints" by pressing the arrow down 2. click "More Error and Warning Handling Options" 3. select tab "Try/Catch Errors" 4. select the second or third option

Matt J
Matt J on 13 Apr 2022 (Edited on 13 Apr 2022)

I wonder what the "Other" methods are that people are using. The score in that category is higher than I expected.

Alyona Buyukli
Alyona Buyukli on 6 Jul 2022
a good old print-statement :D
Faouzi Rahmouni
Faouzi Rahmouni on 4 Apr 2022

The best one is the live script.

DGM
DGM on 1 Apr 2022

I have a simple 4-step approach that usually works.

  1. get so mad that steam comes out of my ears
  2. slam face on keyboard like a cartoon character
  3. roll around on the floor screaming obscenities
  4. troubleshoot the problem and fix it i guess

I hope that someone finds this helpful.

Michael Van de Graaff
Michael Van de Graaff on 27 Jul 2022 at 21:08
huh..... that's a pretty good plan.
Mine is the same but i dont include step 4.
It's a bold strategy, let's see if it pays off!
Sheridan McPheeters
Sheridan McPheeters on 21 Jun 2022
Someone had asked why there were so many "other" answers in the poll and was wondering what "other" debugging methods might be. Asked and answered!
Walter Roberson
Walter Roberson on 31 Mar 2022

dbstop if error can help me locate the place the code bombs, but working backwards from there to locate the cause of the error is mostly breakpoints... and fprintf.

Rik
Rik on 30 Mar 2022

Some older releases don't automatically open the file and move the cursor if you use keyboard, so I use the line below on such releases. It works without incident (even on GNU Octave), as long as I never cd.

ans=dbstack;dbstop('in',strrep(mfilename,'.m',''),'at',num2str(ans(1).line+1)),drawnow %#ok<NOANS>
dpb
dpb on 21 May 2022
I mostly just "let 'er rip!" and see where it bombs, first, then set a breakpoint strategically there or somewhat before. That generally lets me find/correct logic errors within one or two iterations if I don't find I've made an egregious oversight or that makes me realize I should've used a far different approach from the beginning.
I'm the most frustrated with every new release the default behavior on breakpoint/dbstop is to put focus inside the function instead of the workspace...so one instinctively starts to type away and realize you're editing code instead of entering commands. So, have to go change preferences. Why the settings/preferences don't all migrate...
Michael Van de Graaff
Michael Van de Graaff on 21 May 2022
this is why I love keyboard. the cursor is in the console

More Polls

Check out the latest community polls.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!