Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Programmatic shortcuts manipulation – part 1

December 23, 2015 7 Comments

User-defined shortcuts can interactively be added to the Matlab Desktop to enable easy access to often-used scripts (e.g., clearing the console, running a certain program, initializing data etc.). Similarly, we can place shortcuts in the help browser to quickly access often-used pages. Unfortunately, both of these shortcut functionalities, like many other functionalities of the Matlab Desktop and related tools (Editor, Browser, Profiler etc.), have no documented programmatic access.
Such programmatic access is often useful. For example, a large company for which I consult is using centralized updates to users’ shortcuts, in order to manage and expose new features for all Matlab users from a central location. It is easy to send updates and manage a few users, but when your organization has dozens of Matlab users, centralized management becomes a necessity. It’s a pity that companies need to resort to external consultants and undocumented hacks to achieve this, but I’m not complaining since it keeps me occupied…

Shortcuts in Matlab R2012a and earlier
Shortcuts in Matlab R2012a and earlier

Shortcuts in Matlab R2012b and newer
Shortcuts in Matlab R2012b and newer


Today’s post will describe “regular” shortcuts – those that are simple clickable buttons. Next week I will show how we can extend this to incorporate other types of shortcut controls, as well as some advanced customizations.

The shortcuts.xml file

It turns out that the shortcults toolbar (on R2012a and earlier) or toolstrip group (on R2012b onward) is a reflection of the contents of the [prefdir ‘\shortcuts.xml’] file (depending on your version, the file might be named somewhat differently, i.e. shortcuts_2.xml). This file can be edited in any text editor, Matlab’s editor included. So a very easy way to programmatically affect the shortcuts is to update this file. Here is a sample of this file:

<?xml version="1.0" encoding="utf-8"?>
<favoritesroot version="2">
   <title>My Shortcuts</title>
   <favoritecategory>
      <name>Help Browser Favorites</name>
      <favorite>
         <label>Help Using the Desktop</label>
         <icon>Help icon</icon>
         <callback>helpview([docroot '/mapfiles/matlab_env.map'], 'matlabenvironment_desktop');</callback>
         <editable>true</editable>
      </favorite>
   </favoritecategory>
   <favorite>
      <label>CSSM</label>
      <icon>Help icon</icon>
      <callback>disp('No callback specified for this shortcut')</callback>
      <editable>true</editable>
   </favorite>
   <favorite>
      <label>UndocML</label>
      <icon>MATLAB icon</icon>
      <callback>web('undocumentedMatlab.com')</callback>
      <editable>true</editable>
   </favorite>
   <favorite>
      <label>My favorite program</label>
      <icon>C:\Yair\program\icon.gif</icon>
      <callback>cd('C:\Yair\program'); myProgram(123);</callback>
      <editable>true</editable>
   </favorite>
   ...
</favoritesroot>

<?xml version="1.0" encoding="utf-8"?> <favoritesroot version="2"> <title>My Shortcuts</title> <favoritecategory> <name>Help Browser Favorites</name> <favorite> <label>Help Using the Desktop</label> <icon>Help icon</icon> <callback>helpview([docroot '/mapfiles/matlab_env.map'], 'matlabenvironment_desktop');</callback> <editable>true</editable> </favorite> </favoritecategory> <favorite> <label>CSSM</label> <icon>Help icon</icon> <callback>disp('No callback specified for this shortcut')</callback> <editable>true</editable> </favorite> <favorite> <label>UndocML</label> <icon>MATLAB icon</icon> <callback>web('undocumentedMatlab.com')</callback> <editable>true</editable> </favorite> <favorite> <label>My favorite program</label> <icon>C:\Yair\program\icon.gif</icon> <callback>cd('C:\Yair\program'); myProgram(123);</callback> <editable>true</editable> </favorite> ... </favoritesroot>

The file is only loaded once during Matlab startup, so any changes made to it will only take effect after Matlab restarts.

Updating the shortcuts in the current Matlab session

We can update the shortcuts directly, in the current Matlab session, using the builtin com.mathworks.mlwidgets.shortcuts.­ShortcutUtils class. This class has existed largely unchanged for numerous releases (at least as far back as R2008b).
For example, to add a new shortcut to the toolbar:

name = 'My New Shortcut';
cbstr = 'disp(''My New Shortcut'')';  % will be eval'ed when clicked
iconfile = 'c:\path\to\icon.gif';  % default icon if it is not found
isEditable = 'true';
scUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils;
category = scUtils.getDefaultToolbarCategoryName;
scUtils.addShortcutToBottom(name,cbstr,iconfile,category,isEditable);

name = 'My New Shortcut'; cbstr = 'disp(''My New Shortcut'')'; % will be eval'ed when clicked iconfile = 'c:\path\to\icon.gif'; % default icon if it is not found isEditable = 'true'; scUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils; category = scUtils.getDefaultToolbarCategoryName; scUtils.addShortcutToBottom(name,cbstr,iconfile,category,isEditable);

The shortcut’s icon can either be set to a specific icon filepath (e.g., ‘C:\Yair\program\icon.jpg’), or to one of the predefined names: ‘Help icon’, ‘Standard icon’, ‘MATLAB icon’ or ‘Simulink icon’. The editable parameter does not seem to have a visible effect that I could see.
The category name can either be set to the default name using scUtils.getDefaultToolbarCategoryName (‘Shortcuts’ on English-based Matlab R2012b onward), or it can be set to any other name (e.g., ‘My programs’). To add a shortcut to the Help Browser (also known as a “Favorite”), simply set the category to scUtils.getDefaultHelpCategoryName (=’Help Browser Favorites’ on English-based Matlab installations); to add the shortcut to the ‘Start’ button, set the category to ‘Shortcuts’. When you use a non-default category name on R2012a and earlier, you will only see the shortcuts via Matlab’s “Start” button (as seen in the screenshot below); on R2012b onward you will see it as a new category group within the Shortcuts toolstrip (as seen in the screenshot above). For example:

scUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils;
scUtils.addShortcutToBottom('clear', 'clear; clc', 'Standard icon', 'Special commands', 'true');

scUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils; scUtils.addShortcutToBottom('clear', 'clear; clc', 'Standard icon', 'Special commands', 'true');

Custom category in Matlab R2010a
Custom category in Matlab R2010a

To remove a shortcut, use the removeShortcut(category,shortcutName) method (note: this method does not complain if the specified shortcut does not exist):

scUtils.removeShortcut('Shortcuts', 'My New Shortcut');

scUtils.removeShortcut('Shortcuts', 'My New Shortcut');

The addShortcutToBottom() method does not override existing shortcuts. Therefore, to ensure that we don’t add duplicate shortcuts, we must first remove the possibly-existing shortcut using removeShortcut() before adding it. Since removeShortcut() does not complain if the specific shortcut is not found, we can safely use it without having to loop over all the existing shortcuts. Alternately, we could loop over all existing category shortcuts checking their label, and adding a new shortcut only if it is not already found, as follows:

scUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils;
category = scUtils.getDefaultToolbarCategoryName;
scVector = scUtils.getShortcutsByCategory(category);
scArray = scVector.toArray;  % Java array
foundFlag = 0;
for scIdx = 1:length(scArray)
   scName = char(scArray(scIdx));
   if strcmp(scName, 'My New Shortcut')
      foundFlag = 1; break;
      % alternatively: scUtils.removeShortcut(category, scName);
   end
end
if ~foundFlag
   scUtils.addShortcutToBottom(scName, callbackString, iconString, category, 'true');
end

scUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils; category = scUtils.getDefaultToolbarCategoryName; scVector = scUtils.getShortcutsByCategory(category); scArray = scVector.toArray; % Java array foundFlag = 0; for scIdx = 1:length(scArray) scName = char(scArray(scIdx)); if strcmp(scName, 'My New Shortcut') foundFlag = 1; break; % alternatively: scUtils.removeShortcut(category, scName); end end if ~foundFlag scUtils.addShortcutToBottom(scName, callbackString, iconString, category, 'true'); end

As noted above, we can add categories by simply specifying a new category name in the call to scUtils.addShortcutToBottom(). We can also add and remove categories directly, as follows (beware: when removing a category, it is removed together with all its contents):

scUtils.addNewCategory('category name');
scUtils.removeShortcut('category name', []);  % entire category will be deleted

scUtils.addNewCategory('category name'); scUtils.removeShortcut('category name', []); % entire category will be deleted

Shortcut tools on the Matlab File Exchange

Following my advice on StackOverflow back in 2010, Richie Cotton wrapped the code snippets above in a user-friendly utility (set of independent Matlab functions) that can now be found on the Matlab File Exchange and on his blog. Richie tested his toolbox on Matlab releases as old as R2008b, but the functionality may also work on even older releases.

Shortcuts panel embedded in Matlab GUI

Shortcuts are normally visible in the toolbar and the Matlab start menu (R2012a and earlier) or the Matlab Desktop’s toolstrip (R2012b onward). However, using com.mathworks.mlwidgets.shortcuts.ShortcutTreePanel, the schortcuts can also be displayed in any user GUI, complete with right-click context-menu:

jShortcuts = com.mathworks.mlwidgets.shortcuts.ShortcutTreePanel;
[jhShortcuts,hPanel] = javacomponent(jShortcuts, [10,10,300,200], gcf);

jShortcuts = com.mathworks.mlwidgets.shortcuts.ShortcutTreePanel; [jhShortcuts,hPanel] = javacomponent(jShortcuts, [10,10,300,200], gcf);

Shortcuts panel in Matlab figure GUI
Shortcuts panel in Matlab figure GUI

Stay tuned…

Next week I will expand the discussion of Matlab shortcuts with the following improvements:

  1. Displaying non-standard controls as shortcuts: checkboxes, drop-downs (combo-boxes) and toggle-buttons
  2. Customizing the shortcut tooltip (replacing the default tooltip that simply repeats the callback string)
  3. Customizing the shortcut callback (rather than using an eval-ed callback string)
  4. Enabling/disabling shortcuts in run-time

Merry Christmas everyone!

Related posts:

  1. Programmatic shortcuts manipulation – part 2 – Non-standard shortcut controls and customizations can easily be added to the Matlab desktop. ...
  2. Command Window text manipulation – Special control characters can be used to format text output in Matlab's Command Window. ...
  3. Plot-type selection components – Several built-in components enable programmatic plot-type selection in Matlab GUI - this article explains how...
  4. R2009b keyboard bindings – The new Matlab release R2009b includes the ability to customize keyboard bindings for the editor and Command Window. However, there are still some uses for the EditorMacro utility and its variants....
  5. Customizing Workspace context-menu – Matlab's Workspace table context-menu can be configured with user-defined actions - this article explains how....
  6. Variables Editor scrolling – The Matlab Variables Editor can be accessed to provide immediate scrolling to a specified cell location. ...
Desktop Internal component Java Undocumented feature
Print Print
« Previous
Next »
7 Responses
  1. Andi December 23, 2015 at 18:33 Reply

    Yair

    Thank you for addressing the shortcut customization in MatLab.

    The code to update the shortcuts during a session, which is discussed in your article, works very well in MatLab 2012a. However, it does not work reliably in MatLab 2015a, even though I tried to adapt it.

    I have addressed this issue with Mathworks. The response was that they do not support this functionality.

    I have also done some work, where I have tried to update the quick access toolbar, but again found, that is works very unreliably.

    I am very interested in solutions for both issues, which work in MatLab 2015a.

    Thanks, Andi

    • Yair Altman December 24, 2015 at 00:04 Reply

      @Andi – I am not sure what exactly is not working for you in R2015a – it works well for me up to and including R2016a. Try to add a short pause() before you update the shortcuts on startup, maybe this will help. If you would like me to look at your specific issue for a small consulting fee, then email me.

    • Harry July 21, 2020 at 00:20 Reply

      Andi, you are such a tease! There are questions dotted around the web (including on this page) of people asking how to change the Quick-Access Toolbar programmatically, and getting no answer, and here you are saying that you’ve (sort of) done it! Saying it “works very unreliably” implies that it has worked for you at least once; maybe the change didn’t stick, maybe sometimes you get the dreaded red text at the command line and sometimes not – but it sounds like you were at least on the right path. I know that this is a massive long-shot given your post is almost five years old – but, if my post triggers an email to you – please could you tell us how you were updating the Quick-Access Toolbar?

      I know that versions after 2018a now use favourites. Adding favourites to the Quick-Access Toolbar programatically is easy, using the .setIsOnQuickToolBar method of the com.mathworks.mlwidgets.favoritecommands.FavoriteCommandProperties class. However, I am still interested in how to programatically get shortcuts onto the Quick-Access Toolbar in 2017b and earlier. Thus far, I have a partial solution by editing the MATLABQuickAccess.xml file (thanks to Yair and this page for the inspiration for that idea…), but that file is only read at startup so you have to ask the user to restart MATLAB to see your changes, which is less than ideal. And if they don’t restart straight away, they could do things in MATLAB that undo the changes you’ve made to the xml file.

  2. Thierry January 7, 2016 at 15:20 Reply

    Hi Yair.
    Thanks for this post!
    Do you know a way to programmatically assign the created shortcut to the Quick Access Toolbar?
    (This way the shortcuts are also visible on the MATLAB Editor.)
    Many thanks.

  3. Julien November 22, 2018 at 19:29 Reply

    Thanks a lot for these tips, it works very well on Matlab 2016b.
    It seems since the R2018a, shortcuts have disappeared to be replaced by favorites commands… do you have any idea on how to manipulate favorites programatically ?

    I saw this on Matlab Central : https://www.mathworks.com/matlabcentral/answers/411846-how-to-create-favorites-by-code-command-window
    But there is no solution.

    • michel bertrand March 17, 2019 at 20:26 Reply

      There is a solution to create categories and get favorites, code, etc posted on nov 27.
      I am playing with these ideas, and in the process of re-writing the Matlab code I wrote and maintained for more than ten years (following a Yair’suggestions, also many years ago) to save shortcuts when exiting Matlab so as to be able to restore those as they were on a specific day when exiting Matlab; a kind of laymen time machine for shortcuts.

      I found scUtils still “works” in Matlab2018b, but it appears to process something which is distinct from the Favorites that were created when I first launched Maltab2018.
      My first launching of Matlab2018b a few weeks ago followed a Matlab session ending under Matlab 2017b. On its first opening, Matlab2018b did some clean up on shortcuts it found and created their Favorites counterpart using the same name and the same code.
      In my current usage of 2018b, scUtils can still see the old shortcuts and it will give shortcut names and code line exactly the same as those appearing under Favorites, provided those Favorites haven’t been modified with the favorite editor; after a favorite is edited, rerunning scUtils will show the original code, unchanged.

  4. Mike July 3, 2020 at 21:24 Reply

    Yair,

    It may be July 2020 but at my company we are still using R2013b! This article was instrumental in helping me to accomplish my work.

    Thanks!
    Mike

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top