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:
Related
So I have built some custom macros within my main script (where I cobble together my usual tools) triggered by hotkeys. I am starting to accumulate these macros and would like to turn off certain sections of the script so I can use the hotkey again and can collapse the code section in my editor. I don't want to create separate scripts for these custom functions (no particular reason tbh).
I have tried wrapping the section of script in an if statement but it simply executes the code regardless of the condition (see below)
if(0){
^!s::
<my script>
return
}
From how I understand it the ^!s:: hotkey is compiled even if it is within a failed conditional and triggers the actions of the script regardless. Would this be the correct understanding? Is there a way to "turn off" that section of script without commenting it out?
#EJE & #Joe DF
I believe the #If was what I was looking for. Thank you.
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'
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.
A lot of the code that I write in Matlab has a very verbose output. As the program runs, information is printed to the command window, and with each new line, the window automatically scrolls to the bottom. This becomes a problem when I want to read some of the output more closely or scroll up to look at older output. I can scroll up, but only until a new line is printed, which is often less than a second.
Does anyone know if it is possible to turn off this automatic scrolling in the Matlab window? I work in a number of different Matlab versions, depending on the machine, and this happens with all of them. The answer to this might be "No", but I swear I remember having this functionality at one point.
Use the more function: http://www.mathworks.com/help/matlab/ref/more.html
more on
Then run your program. Press spacebar when you wish to see more of the output.
more off will turn it off.
You may find this workaround useful.
First launch matlab using the command line matlab -logfile 'myLog.txt' (the doc says it "starts MATLAB and makes a copy of any output to the Command Window in filename. This includes all crash reports.")
Then open your .txt file using a text editor supporting automatic refresh of content (see picture). On OSX I use TextWrangler (freely available at www) but others have been reported to have this feature (see here or here).
Results: output displays (fprintf, disp, but not the commands per se) are printed both on the Matlab console and the text editor (file is refreshed with a little lag time, below half a second I would say with my configuration). And there is no automatic scrolling. Such procedure does not seem to impact the overall performance of the script (although it may deserve some testing).