September 7, 2016

MATLAB Editor: customized Shortcuts

It is possible to add Shortcuts in the MATLAB Editor for customized actions. I take as example in this post the nice utility in the File Exchange plot_depfun.

1. Function returning current file in Editor

For this you need a utility function that returns the current opened file in the MATLAB Editor. You can find this function below: As you see this can be achieved in lines of code. (Credit to Yair Altman http://UndocumentedMatlab.com)

2. Adapt function to take as default input file the current file in the Editor

Then you need to adapt the function to be run by the Shortcut to take by default the current file in editor, calling previous utility function. You need to add something like the lines below:
if nargin==0 || isempty(foo),
    foo= EditorGetFile();
end
at the top of the file. (My full modified plot_depfun version is available in Gist here.)

3. Create the Shortcut

You can create a Shortcut in the MATLAB Toolbar as explained in the& MATLAB documentation. See screenshot below:
Screenshot - Create MATLAB Shortcut with "Add to quick access toolbar" selected
Create Shortcut with "Add to quick access toolbar"
Simply put a call to the plot_depfun function as Callback: this will take the current file in the Editor (see modification done in Step 2.) If you haven't implemented in the function the default used file as EditorGetFile (Step 2), you could also simply use here as Callback plot_depfun(EditorGetFile);. Important: Check the option "Add to quick access toolbar". This will make the Shortcut visible also in the MATLAB Editor as shown in next screenshot in the so-called Quick Access Toolbar:
Screenshot - Shortcut visible in MATLAB Editor Quick Access Toolbar
Shortcut is accessible from Editor Quick Access Toolbar
Hint 1: You can organize the Shortcuts (re-order, add separators etc.) by right-clicking on the Toolbar and select the last context menu entry "Customize...". Hint 2: The file located under fullfile(matlabroot, 'toolbox/matlab/icons','plotpicker-dendrogram.png') is used as icon for the plot_depfun shortcut.

No comments:

Post a Comment