Matlab: Is there a command to set 'code folding' preference? - 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.

Related

Programmatically highlight dialog option in Simulink

In Simulink, certain error messages provide clickable links to the origin of the error.
See, for example, the following error:
If I click one of the three links in the message, say, Parent setting, Simulink opens the code generation settings dialog and indicates the specific setting(s) using a blue border:
As a user, can I achieve the same highlighting programmatically (using a .m-script)? If so, how?
Yes, this is possible, but it requires the use of Simulink internal support functions. Their API is undocumented and may therefore be unstable. Use at your own risk.
The highlighting in the question is achieved as follows:
slprivate('modelref_highlight_configset_setting', 'rtwdemo_capi', 'RTWCAPISignals')
Here,
slprivate is a .m-function that ships with Simulink. There is no help entry for the function. Its only mention in the help is on the 'Set Simulink Preferences' help page. Its implementation is simple: it is a wrapper around feval. The implementation can be opened by executing the command >> edit slprivate from the Matlab Command window.
The function's documentation is as follows:
slprivate is a gateway for internal support functions used by Simulink.
VARARGOUT = slprivate('FUNCTION_NAME', VARARGIN)
In the usage above, the first parameter 'modelref_highlight_configset_setting', is the Simulink internal support function. In this case, it is the function that does the highlighting.
The second parameter 'rtwdemo_capi', is the name of the Simulink model whose Configuration Parameters window should be opened for highlighting.
The third parameter, 'RTWCAPISignals', is the name of the configuration option to highlight. In this case, that single option highlights two UI elements.
Names of configuration options can be found as follows:
Open the model's Configuration Parameters window (e.g. in Simulink: menu Simulation -> Model Configuration Parameters, or Ctrl + E)
Right-click on (or, rather, next to) the option
In the context menu that appears, click What's This?
In the help window that appears, scroll down to Command-Line Information. The name of the option is given in the Parameter field.
Edit:
The way I figured this out may be useful for other internal functions, so I'll leave that here as well. If the build is started from the Command Window (>> rtwbuild('rtwdemo_capi')) instead of from the GUI, warning and error messages are printed to the command window as well, including the clickable links. If one then hovers over such a link with the mouse pointer, the corresponding command is shown in the Matlab status bar (at the bottom of the main Matlab window).

Matlab Run Section not working

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:

Matlab control+enter key on figure

I want to capture when the user holds down the control key and presses the enter key on a figure window. Note: This is the default keys for "Evaluate Current Section" in the editor.
See example code below:
function test
f = figure;
f.KeyPressFcn = #myKeyPressFcn;
end
function myKeyPressFcn ( hFig, event )
cm = hFig.CurrentModifier();
if ~isempty ( cm )
fprintf ( 'CurrentKey: %s:%s\n', cm{1}, hFig.CurrentKey );
else
fprintf ( 'CurrentKey: %s\n', hFig.CurrentKey );
end
end
To reproduce save the above in an active file in the editor and run the function - the editor needs to be open (this is important!!).
With the figure active press any key -> the selected key is written to the terminal window. However if you hold down Control and press the enter (return) key then this is not captured but instead Matlab attempts to "Evaluate Current Section" (or cell as it used to be called) in the editor (no matter that the figure has the focus). This of course throws as error...
I have contacted Matlab support and this is "expected behaviour!". I can (just about) see why it might be a good idea for demos - but for professional applications that run in Matlab I personally think this "feature" is a bug!!
Notes
When the editor is closed the control+enter is captured in the figure
In deployed applications the control+enter is captured.
If I manually change the Evaluate Current Section shortcut then control+enter is captured.
I have tried a number of things to resolve this but none have worked, for example hiding the editor or setting editor enable state to false (neither of these are acceptable solutions - I was trying to see what I could get to work on a small test case...):
function test
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
jEditor.setVisible(false);
jEditor.setEnable(false);
f = figure
f.KeyPressFcn = #myKeyPressFcn;
uiwait(f);
jEditor.setVisible(true);
jEditor.setEnable(true);
end
The only way I can get it to work is to close all of the editor files on launching the GUI and then opening them again when the GUI closes (this is not an acceptable solution... - for fairly obvious reasons!!)
I did think about trying to temporarily modify the shortcut key (Preferences-Keyboard-Shortcuts) of the "Evaluate Current Section" -> but haven't worked out a way to do it from the commandline, and then set it back again when finished. If this is fast you could do it when the user presses and releases the control key.
So what am I asking:
If possible I need a solution that will work for anyone anywhere - as if I can get this to work it will be included in a new add-on feature in my Matlab GUI Toolbox. - which is used by people all over the world.
Do you know how to modify the keyboard shortcuts from the commandline - if so how!
Any other suggestions?
My other idea is to change my shortcut to a different key combination - but wheres the fun in that! :) (I will still have the issue if some user somewhere has altered the execute the current cell to my new combination...)
P.S. Thanks for reading this far down!! :)
Why don't you go to the home> Preferences > keyboard > Shortcutand change it there?
you only need to hit Ctrl + Enter in the black box at top of the page for searching the related command, which is here Evaluate Current Section and change it whatever you like.
Please bear in mind you will only need to split out your windows (Undock them). Then, when you click on Ctrl + Enter, it will do whatever you would like.
I hope you find this answer helpful.
You can try the solution from my FEX submission. The KbTimer is motivated by the need to capture keyboard stroke without the need of GUI that designed either by GUIDE or APP DESIGNER. Note that the implementation of function was inspired from the PsychToolbox which is MEX based.

Using IntelliJ IDE to autocomplete line and place semi-colon

In eclipse you can hit "enter" and the IDE will automatically take you to the end of the line and place a semicolon.
In IntelliJ, if you hit shift-enter you get similar behavior minus adding the semicolon. I have read and tried cntrl-shift-enter and you get the exact same behavior. However, that is such an awkward key combination to be using all the time, at least much more so than the one-button approach using Eclipse.
Any ideas?
IntelliJ autocomplete escape
In InteiJ IDEA use (Ctrl-Shift-Enter) for "Smart Autocomplete".
It will end the line "smartly" with a semicolon as you wish. It also works in a few different situations like IF statements of FOR loops.
Why don't you redo the key binding?
Go to: (spanner icon) Settings -> Keymap -> Complete Current Statement and rebind how you will (for example, ctrl-enter might suit you better).
Or there's also a default keymap set for Eclipse that you can choose. I'm not sure if it affects the complete-current-line action, but you could take a look.
you can also use ctrl+shift+space
if you want always have it just do this

In Emacs, how can I jump between functions in the current file?

I'd like to quickly move point to a function in my Emacs buffer. I'd like to run some function and get a prompt asking me for the function name, with completion provided for every function defined in the current buffer.
I generally use etags to navigate around, but sometimes I'm looking for a framework method that's been overridden in several files. In these cases, I can find the file I need but then I'd like to quickly jump to the function there. There is a similar feature in TextMate where you can select a definition from a list in the bottom right of the editor.
Just to jump around functions in the current file? Use imenu. It's the simplest and lightest of all the alternatives listed so far and might be enough for what you want. It's also built into Emacs and has minimum setup hassle. It features graphical and textual interfaces. Anything extra and you'll be better off using one of the other excellent suggestions made here.
speedbar comes standard, and gives you a collapsible menu for each file in the current directory, by default middle clicking on an entry for a function definition jumps to that def. With emacs23 this was changed to the more normal leftclick.
You can use etags-select to select from multiple matching tags. But the answer to what you asked is imenu.
Icicles is probably closer to what you are looking for:
http://www.emacswiki.org/emacs/Icicles_-_Tags_Enhancements
It's an enhancement to etags and includes (among other things) the file name with the tag so you can tell if it's the one you are looking for.
try CEDET. It is a bit difficult to set up the first, but here is an excellent tutorial: by Alex ott
And when he gets installed, you can use semantic-complete-jump. pressed tab couple times, and it is also brings up symbol definitions.
If M-. brings up the wrong method, you can type C-u M-. to find the next one with the same name.
global gtags is very good
To navigate within the current file or a set of files that you select, you do not need a TAGS file. You can use Imenu. But it is better to use Icicles imenu commands.
Why? Because they let you use completion. Substring, regexp, prefix, or fuzzy completion. Combine simple patterns to match, or subtract them.
Command icicle-imenu is bound in Icicle mode to C-c =. Butyou can also look up just a command or just a non-command function (non-interactive), using command icicle-imenu-command or icicle-imenu-non-interactive-function.
These commands are multi-commands, meaning that they are actually browsers: you can trip among function definitions using keys C-RET or C-mouse-2 (direct jumps) and C-down (cycle). Hit RET or click mouse-2 to settle down at a final destination.
I use C-M-a and C-M-e to jump between the beginning and end of functions.
Otherwise, open up Speedbar and click the + icon next to a file name to view a list of functions contained in the file. Then click on the function names to jump to them directly.