A shortcut to success
The MATLAB user interface is laid out in a way so you spend a fair amount of time with one hand on your mouse. When that happens, toolbar shortcuts are a great way to run chunks of code that you use regularly. The trouble is, in order to set them up, you need to do lots of clicking to interact with the MATLAB UI. This has two big problems. Every time you install a new copy of MATLAB, you need to set up your shortcuts again. Click, click, click. Sigh. It also means that shortcuts are difficult to share with your colleagues/friends/that hot chick you want to impress with your scientific coding skillz.
The solution to this is to generate the shortcuts programmatically. Write your shortcut script once, and you can reuse the code to your heart’s content. The trouble is, MATLAB doesn’t ship with any tools to manipulate shortcuts. All the details of your shortcuts (that’s the toolbar shortcuts your help browser favourites, and any other shortcuts you care to define) are stored in the file
shortcuts.xml in your preferences directory (as returned by
There are two ways to write methods to add and remove shortcuts. You can use some XML tools, of which my favourite is the xml_io_tools package by Jaroslaw Tuszynski. The other alternative is to delve into the mostly undocumented MATLAB UI functions. These are mostly built out of Java Swing widgets, and while the documentation is sketchy to non existent, it is possible to utilise their features. I don’t want to dwell on the technicalities of these widgets; if you want to know more then Yair Altman’s Undocumented Matlab blog is the best place to look.
My shortcut tools package uses this second method. It contains methods for adding, moving and removing toolbar shortcuts and browser favourites, and a few other utility functions. I going to discuss two possible use cases for the toolbox, one really simple and another that’s slightly more advanced.
First of all, we need some code to be called by the shortcut. This is a very simple function to tidy up your workspace:
%TIDY Clear workspace and command window; close plots.
close all hidden;
MATLAB secretly ships with a set of icons; I’m using a cross, which is available from R2009a onwards. Feel free to use any icon you like, or leave the icon argument blank to use the standard icon. Adding a shortcut is as easy as calling
iconPath = fullfile( ...
AddShortcut('Tidy', 'tidy();', iconPath);
You should see the shortcut appear in your toolbar. If you decide you don’t want this shortcut, you can remove it with
(The first parameter requests the category of shortcut, which by default is
'Toolbar Shortcuts'. We don’t want to change this, so we can just leave it empty.)
An idea that was suggested to me by Iram Weinstein was to have different shortcuts available when you work on different projects. This requires a little more setting up, but when you can script it, it’s easy enough to do. Let’s imagine that you like to work on both cellular automatons and 3D graphics. Some shortcuts you use will be needed by both projects, but others will be specific to each research area.
First we need to create some categories to contain the shortcuts; one for each project and one to contain the shortcuts common to all your projects.
project1 = 'Cellular Automaton Project';
project2 = '3D Graphics Project';
common = 'Common';
Then we add shortcuts to each category, using
AddShortcut, like before. You’ll probably want several shortcuts per project, but for now let’s stick to one shortcut for each.
AddShortcut('Life', 'life();', , project1);
AddShortcut('Teapot', 'teapotdemo();', , project2);
AddShortcut('Tidy', 'tidy();', iconPath, common);
AddShortcut('Cellular', 'SetupCelluarProject();', , common);
AddShortcut('Graphics', 'SetupGraphicsProject();', , common);
In the common section, at the very least you need a shortcut to initialize each project. All that is left is to write the callback functions for those shortcuts.
%SETUPCELLULARPROJECT Sets up the Cellular Automaton Project.
% Get rid of work from other projects.
% Clear the existing toolbar
% Copy the shortcuts from the common category and the project category
% to the toolbar. (The toolbar is the default target location.)
CopyShortcuts('Cellular Automaton Project');
% Other setup code goes here. You'll probably want a call to cd here to
% move to the project directory, and perhaps addpath or load as well.
SetupGraphicsProject will contain similar contents. By encapsulating this setup code into a function, it means that you can call it from the command line to get going with your project as well. As a lazy typist I tend to use shorter function names for these, or at least have a short alias.
That’s it. You can now update your shortcuts at the click of a button.