how to trigger breakpoint by command in matlab - matlab

I have an if state in my code where if something went wrong and I want to debug if it hits there. similar to manually set breakpoint on that line but permanent.
so it will always have that red dot over there by command..
I've seen some dbstop commands but they are all ends with in file or if error..
I couldn't find a basic 'dbstop here' or something..
Is that possible?
Thanks.

Take a look at the keyboard command. It will not create the red dot, but it basically behaves like a breakpoint.

dbstop has more arguments than just in file or if error:
dbstop in file at location sets a breakpoint at the specified location. MATLAB execution pauses immediately before that location, unless the location is an anonymous function. If the location is an anonymous function, then execution pauses just after the breakpoint.

Related

Create a matlab debugger breakpoint and enter debugger in .m file code

How can I create a matlab debugger breakpoint on the current line of a .m matlab file, cause matlab to enter the debugger at that point? I.e., pause the current code execution and drop into the debugger?
This would help me because I edit matlab files in an external editor. Wen I save a file, matlab clears any breakpoints I have set in that file. I have tried:
dbstop at [current_file.m]
which does not work because it sets breakpoint at the first line in the file.
As an example, in python, this could be done by:
import IPython
IPython.embed()
You can use dbstop to stop at a specific line:
dbstop in FILESPEC at LINENO
help dbstop gives you all the options.
You can use keyboard to stop at a certain point in the code.
The only problem is that it requires editing the code each time you want to add\remove a breakpoint

Retrieve a variable from a function that crashed matlab

Is there a way to retrieve the output of a function that did not complete properly?
For instance, a (non global) variable that was correctly computed by a function but that couldn't be saved properly because of syntax errors.
In principle, you can't look into a function once the program has stopped with an error. (That's why I often try to avoid functions.)
However, you can achieve what you want by entering debugging mode, using the dbstop function to set a breakpoint:
The dbstop function is used to temporarily stop the execution of a
program and give the user an opportunity to examine the local
workspace.
In particular, typing
dbstop if error
in the command window before running your code will make it stop at the point that caused the error and look at the variables within that function.
To restore normal behaviour you need the dbclear function. Type
dbclear if error
to remove the previously set breakpoint, or
dbclear all
to remove all breakpoints.

Can you pause MATLAB? [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 6 years ago.
In MATLAB, I'm running some code which takes a while to run. I'd like to pause the code to check on some variable values. Is there a way I can do this without having to re-run the code from the beginning? I don't want to terminate the program; just pause it.
You can halt execution and give a command prompt in two ways of which I am aware:
Putting keyboard in your code where you want to stop.
Setting a breakpoint.
You can resume and stop execution with dbcont and dbquit, respectively. To step forward, use dbstep. dbstack lets you see where you are. There are many more commands. The help page for any of these will give you other suggestions.
As Dennis Jaheruddin has pointed out, dbstop also has several useful features worth trying. In particular is the ability to set conditional and global (any line meeting a criterion) breakpoints via the dbstop if syntax. For example, dbstop if error will break to a debugging command prompt on any error. One suggestion he made, which I now do, is to put dbstop if error into startup.m so that this behavior will be default when you start MATLAB. You may need to create this file in a userpath folder; edit(fullfile(regexp(userpath,'^[^;]*','match','once'),'startup.m')).
One way to achieve what you're looking for would be to use code sections (also known as code cells), where you divide your code into sections divided by lines with two percent signs (%%).
Then, in the editor, you can press ctrl+enter to execute the current code section, and ctrl+up/down to navigate between sections.
Well there is the pause command, but then you cannot check for the variable contents in the workspace because the program is running.
What you probably want is to set a breakpoint (See the Debug menu / key F12).
At a breakpoint matlab pauses the program and enters debugging mode in which you can see and edit the variables. Once finished, you can resume the program where it was paused.
I'm not sure about Windows users but if you're running Linux you can start Matlab in a terminal using
matlab -nodesktop
then once Matlab has started, cd to your project directory and start your Matlab script. Now whenever you want to pause execution you can use ctrl-Z. Then to resume type fg. I hope this helps.

Difference between script and matlab command window

I wonder what the difference is between entering a few lines in the command window, or letting a script execute them.
In the question Escape from nested try - catch statement I have an example function. I have put the selected code in a script and called it, however then it does not work properly. On the other hand, when I
select the lines and hit f9, it works as expected.
The lines are:
dbclear all
dbquit
dbstop if caught error
I call the example function as such:
dbstop if caught error
mytestmain
And the example function is:
function mytestmain
try
mytestsub
catch
end
% Definition of subfunction, may or may not be in the same .m file
function mytestsub
try
a=b; %Intentionally generate an error as b is not defined
catch
end
I think it's related to MATLAB's just-in-time (JIT) compiler, which compiles functions before it runs them.
It seems that it compiles functions differently if dbstop is set or not (see here for reference). As it currently stands, MATLAB can not recompile a function while it is run (just try saving a changed function during a dbstop, and you will get a message informing you). As you can add and remove breakpoints during a dbstop I think you can also do so programmatically, but it should be impossible to "turn on" debugging if it wasn't turned on at "compile time"
So in your cases:
Using F9 it's just pasted and parsed as if you input it manually. So first dbstop is set, then mytestmain gets compiled and executed.
Running as a script will first compile the script and mytestmain and then execute it - so dbstop would be set after compilation and therefore not in effect.
Depending on what you mean by "doesn't work", it could just be because the debugger is a special context and certain debugger commands - dbup, dbdown, and dbquit - only work when you're at a debugger "K>>" prompt. Once you call a script, you're no longer at the debugger prompt but in normal code execution - inside a nested M-code call stack - and they just don't work there. When you F9, it does the lines individually, so each one is done from the prompt.
As a workaround, if you really want to execute a sequence of debugger commands like this, you could write a little Java Swing widget to enter the text in to the command window just as though you were typing it in.

How to inspect a variable in XCODE?

Is there any command using which we can inspect a object in command line while app is running in DEBUG mode. I do not want to put description message in the code.
Try these resources. one two
(gdb) p varName
Yes sure. If you are debuging, breakpoints are automatically set to on. Just set a breakpoint to the line in which the variable is. The program stops as soon it reaches the line with the breakpoint. Just hold the cursor over the variable and all important data is displayed. I do it also that way all the time. ;-)