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

Set breakpoints
Run with "Pause On Errors"
The dbstop() command
Pause the code with keyboard()
Get help from Matlab Community
2506 票

Michaela Maier
Michaela Maier 2022 年 7 月 27 日
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 2022 年 7 月 16 日
I go to documentation and answers.
Sheridan McPheeters
Sheridan McPheeters 2022 年 6 月 21 日
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 2022 年 4 月 25 日
Learning about the keyboard command was life changing for me
Matt J
Matt J 2022 年 4 月 27 日
But why not dbstop, or inserted break points?
Michael Van de Graaff
Michael Van de Graaff 2022 年 5 月 21 日
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 2022 年 4 月 20 日

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 2022 年 4 月 20 日

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 2022 年 4 月 13 日 (編集日時: 2022 年 4 月 13 日)

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 2022 年 7 月 6 日
a good old print-statement :D
Faouzi Rahmouni
Faouzi Rahmouni 2022 年 4 月 4 日

The best one is the live script.

DGM 2022 年 4 月 1 日

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 2022 年 7 月 27 日
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 2022 年 6 月 21 日
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 2022 年 3 月 31 日

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 2022 年 3 月 30 日

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 2022 年 5 月 21 日
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 2022 年 5 月 21 日
this is why I love keyboard. the cursor is in the console