In Matlab, is it possible to set a data breakpoint on a specific variable as it is done in Visual Studio? I could not find anything online and in the manual, can really such a important feature be missing?
It does its called conditional breakpoints.
You can set them at the command line but its a lot easier to put them in interactively via the editor (right click on "-" next line number and select "Set Conditional Breakpoint"
Commandline:
dbstop in FUNCTION at LINENO if 'EXPRESSION'
Related
I encountered an annoying problem with Matlab 2016b: I cannot run folded code sections separately. More specifically, in Matlab editor buttons "Run and Advance" ans "Run Section" appear grey (unclickable) and Ctrl+Enter to run a code section does not work either.
My script does recognize folded code sections (separated with %%) just fine and I am able to use the "Advance" button to move between folded sections, so it seems the problem is not some syntax error hidden in the code. I am also able to run the script in the usual way with "Run" and even run selected lines of code with F9 command. I would, however, want to avoid using the latter option as "Run Section" and "Run and Advance" options are quite handy and do not require highlighting lines of code.
Any ideas what could cause the problem?
The root of the problem was my auxiliary functions defined within the main script. Since these functions were located in one of the sections, I couldn't call them from other sections separately. The solution was to save auxiliary functions as separate m-files. The following image highlights the problem:
Question: Is there a preferred way to debug functions in matlab?
If I all calculations in a script, then every variable is in the workspace and I can easily query them to find out what's not working right. However, if I use a function, then only my outputs are visible and I can't see what's going wrong. I understand there are ways of getting around this, but thusfar they seem to be more trouble than just making one, long ugly, script. So how do YOU debug functions in matlab? Is there a preferred/efficient way of doing this?
I always make sure to enable "Stop If Error" in the Breakpoints menu and if I want to debug a specific function I set a breakpoint at the first line in that function (or at the point of interest). Note that "clear all", which is common in the beginning of scripts deletes all break points. Use "clear variables" instead.
See MATLAB settings - stop if errors for more info on how to make the Stop If Error persist when you restart Matlab.
I'm using the %% command to split my code into blocks for the sake of readability,
I collapse those blocks when i'm not working in them.
The default settings don't give you the ability to fold them, so my first question is:
Can you tweak this setting by command, so anyone who opens my script has the option to fold them?
My second question is: Can I program my code to set the blocks as collapsed by default?
Thanks in advance
It is indeed possible
1) Type preference in the command window to get up the preferences menu (or you find in under home)
2) Go to Editor/Debugger -> Code Folding
3) Mark the enable box for Sections
You could wrap your sections in the following way:
%% //Section header
for folding=true
%// Your code here
end %//folding
This allows you to fold on the for "loop".
It will work for everybody who has a fairly recent Matlab editor, without messing with editor settings.
Note that you should not have an actual variable named folding.
Yes, this is entirely possible:
com.mathworks.services.Prefs.setBooleanPref('EditorMCodeFoldEnabledcell', true);
The command takes effect immediately. Find more information in the article Changing system preferences programmatically.
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.
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:
The M-file in which to stop
Line number
Sub-function
Conditional to an arbitrary expression. For example,
dbstop in myFun.m at 224 if ~exist('x','var')
At any run-time error (dbstop if error)
At a specific error (e.g dbstop if error myFun.m:barErrorId)
At any warning (dbstop if warning) or specific warning
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.
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.
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.
You can use MATLAB's editor debug button to debug within MATLAB environment
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you retrieve the selected text in MATLAB?
I want to implement and add some features to the function "Evaluate Selections", where you can highlight code and then "Evaluate Selections" by right click your mouse (or F9).
In the editor environment, this is how it is done:
editorObject = matlab.desktop.editor.getActive;
eval([editorObject.SelectedText ';']);
How can I implement this from the command line window, or the help window?
EDIT:
Maybe I didn't express my question clearly.
Imagining that we already have this function called eva_select(), I can use function this way:
I wrap the function as the Shortcuts button.
Use mouse to select a variable at command line window, maybe I entered before, say var_a
Then I click that Shortcuts button, the text which I selected before will be executed. This is exactly as press F9 key or choose right mouse menu -- "Evaluate Selections".
But if we really have that function, we can do more! We can modified eva_select() to eva_select_size(), in this way, we can select a variable, say var_a at command line window or help window, click eva_select_size() shortcuts button, then, we will get size(var_a) at command line window!
EDIT:
Thanks, I can retrieve the text in the command window, but I can't do the same thing in the help window, is it possible to do that?
The command window, like other GUI components in the MATLAB desktop, is Java-based. Therefore it can be accessed programmatically, but it is completely undocumented and its use is not officially supported.
Exploring around, here is a solution that seems to work in both R2012a and R2012b. It involves obtaining a handle to the underlying JTextArea of the command window, which is used to get the selected text (to evaluate size of selected variable name)
Create a shortcut with the following code:
x = com.mathworks.mde.cmdwin.XCmdWndView.getInstance();
s = char(x.getSelectedText());
if isvarname(s) && exist(s,'var')
eval( sprintf('size(%s)',s) );
end
Next highlight a variable name in the command window and execute the shortcut. The size will be immediately printed as shown in the screenshot below:
It is not very nice as it is an external solution, but this is how it could work:
Assuming you are in the command window and want to evaluate size(var_a) by selecting it, you can probably do this with a keyboard macro. Defind the appropriate function of var_a
f(x) = eval('size(' x ')'
%This could be done in the macro, but nicer to do it here for easy editing.
Then make sure your macro does this:
Copy 'var_a'
Turn it into 'f(var_a)'
Paste the result
Hit enter
Like i said, it's not pretty, but it should do the trick.