- Undocumented Matlab - https://undocumentedmatlab.com -

Modifying default toolbar/menubar actions

Posted By Yair Altman On June 2, 2010 | 9 Comments

Did you ever wish to modify Matlab’s default toolbar/menubar items?
I recently consulted to a client who needed to modify the default behavior of the legend action in the toolbar, and the corresponding item on the main menu (Insert / Legend). The official version is that only user-added items can be customized, whereas standard Matlab items cannot.
Luckily, this can indeed be done using simple pure-Matlab code. The trick is to get the handles of the toolbar/menubar objects and then to modify their callback properties.

Default legend insertion action
Default legend insertion action

Using the toolbar/menubar item handles

Getting the handles can be done in several different ways. In my experience, using the object’s tag string is often easiest, fastest and more maintainable. Note that the Matlab toolbar and main menu handles are normally hidden (HandleVisibility = ‘off’). Therefore, in order to access them we need to use the findall function rather than the better-known findobj function. Also note that to set the callbacks we need to access differently-named properties: ClickedCallback for toolbar buttons, and Callback for menu items:

hToolLegend = findall(gcf,'tag','Annotation.InsertLegend');
set(hToolLegend, 'ClickedCallback',{@cbLegend,hFig,myData});
hMenuLegend = findall(gcf,'tag','figMenuInsertLegend');
set(hMenuLegend, 'Callback',{@cbLegend,hFig,myData});

As a side note, two undocumented/unsupported functions, uitool(hFig,tagName) and uigettoolbar, also retrieve toolbar items. Since using findall is so simple and more supported, I suggest using the findall approach.
To see the list of available toolbar/menubar tags, use the following code snippet:

% Toolbar tag names:
>> hToolbar = findall(gcf,'tag','FigureToolBar');
>> get(findall(hToolbar),'tag')
ans =
    'FigureToolBar'
    'Plottools.PlottoolsOn'
    'Plottools.PlottoolsOff'
    'Annotation.InsertLegend'
    'Annotation.InsertColorbar'
    'DataManager.Linking'
    'Exploration.Brushing'
    'Exploration.DataCursor'
    'Exploration.Rotate'
    'Exploration.Pan'
    'Exploration.ZoomOut'
    'Exploration.ZoomIn'
    'Standard.EditPlot'
    'Standard.PrintFigure'
    'Standard.SaveFigure'
    'Standard.FileOpen'
    'Standard.NewFigure'
    ''
% Menu-bar tag names:
>> get(findall(gcf,'type','uimenu'),'tag')
ans =
    'figMenuHelp'
    'figMenuWindow'
    'figMenuDesktop'
    'figMenuTools'
    'figMenuInsert'
    'figMenuView'
    'figMenuEdit'
    'figMenuFile'
    'figMenuHelpAbout'
    'figMenuHelpPatens'
    'figMenuHelpTerms'
    'figMenuHelpActivation'
    'figMenuDemos'
    'figMenuTutorials'
    'figMenuHelpUpdates'
    'figMenuGetTrials'
    'figMenuWeb'
    'figMenuHelpPrintingExport'
    'figMenuHelpAnnotatingGraphs'
    'figMenuHelpPlottingTools'
    'figMenuHelpGraphics'
    ''
    ''                    < = Note the missing tag names...
    ''
    'figMenuToolsBFDS'    <= note the duplicate tag names...
    'figMenuToolsBFDS'
    'figDataManagerBrushTools'
    'figMenuToolsAlign'
    'figMenuToolsAlign'
    ... (plus many many more...)

Unfortunately, as seen above, Matlab developers forgot to assign tags to some default toolbar/menubar items. Luckily, in my particular case, both legend handles (toolbar, menu) have valid tag names that can be used: 'Annotation.InsertLegend' and 'figMenuInsertLegend'.
If an item's tag is missing, you can always find its handle using its Parent, Label, Tooltip or Callback properties. For example:

hPrintMenuItem = get(findall(gcf,'Label','&Print...'));

Note that property values, such as the tag names or labels, are unsupported and undocumented. They may change without warning or notice between Matlab releases, and have indeed done so in the past. Therefore, if your code needs to be compatible with older Matlab releases, ensure that you cover all the possible property values, or use a different way to access the handles. While using the tag name or label as shown above is considered “Low risk” (I don’t expect it to break in the near future), their actual values should be considered somewhat more risky and we should therefore code defensively.

Another simple example: Print Preview

As another simple and very useful example, let’s modify the default Print action (and tooltip) on the figure toolbar to display the Print-Preview window rather than send the figure directly to the printer:

Matlab's default toolbar Print action
Matlab's default toolbar Print action

hToolbar = findall(gcf,'tag','FigureToolBar');
hPrintButton = findall(hToolbar,'tag','Standard.PrintFigure');
set(hPrintButton, 'ClickedCallback','printpreview(gcbf)', 'TooltipString','Print Preview');

Matlab's Print Preview window
Matlab's Print Preview window

More advanced customization of the toolbar and menu items are possible, but require a bit of Java code. Examples of toolbar customizations were presented in past articles (here [1] and here [2]). A future article will explain how to customize menu items.
Have you found a nifty way to customize the menubar or toolbar? If so, please share it in the comments section [3] below.

Categories: Figure window, Handle graphics, Low risk of breaking in future versions, Stock Matlab function, Undocumented feature


9 Comments (Open | Close)

9 Comments To "Modifying default toolbar/menubar actions"

#1 Comment By lux On March 15, 2011 @ 10:25

Hello Yair!
I would like to ask you in what way I can alter the Standard.PrintFigure properties so as to be valid for every figure window and not only for the current one(gcf).
Like getting assimilated into the default figure toolbar.
Thank you for your help.

#2 Comment By Yair Altman On March 16, 2011 @ 05:57

@lux – there is no official way to do this, but of course (as always) there’s a back-door: you can simply add your code to the printdlg.m file. This will get executed whenever the user tries to print any figure.

In Matlab releases up to R2009b you can add your code to the built-in figureToolbarCreateFcn.m function. Unfortunately, this back-door was closed in R2010a onward…

You should also take a look at [10], which explains how to programmatically modify the default print setup.

#3 Comment By Andi On June 29, 2011 @ 10:32

Hello Yair,

Is there a way of performing these sort of modifications on the main Matlab toolbar rather than on figure toolbars.

Thanks for your help

#4 Comment By Yair Altman On June 29, 2011 @ 14:49

@Andi – Of course you can modify the main Matlab desktop toolbar, just like a figure toolbar. Here is one way to get a reference to the toolbar container, which is a standard com.mathworks.mwswing.MJToolBar object:

hMainFrame = com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame;
hToolbar = hMainFrame.getContentPane.getComponent(0).getComponent(0);

You may of need to adapt the code for docking etc., but this should be enough to get you started.

#5 Comment By klimmbimm On July 28, 2011 @ 03:59

Is there a way to get a cut push button to my figure?

#6 Comment By Yair Altman On July 28, 2011 @ 12:12

@klimmbimm – adding simple buttons to the figure toolbar can be done using the uipushtool function

#7 Pingback By Customizing figure toolbar background | Undocumented Matlab On March 14, 2013 @ 08:43

[…] I need to manually remove several unuseful toolbar controls. I do this by directly accessing the toolbar control handles […]

#8 Pingback By Another couple of Matlab bugs and workarounds | Undocumented Matlab On December 10, 2014 @ 15:06

[…] image to a Matlab variable (2D RGB image), an image file, system clipboard, or the printer. We can easily modify the <Print> toolbar button and menu item to use this utility rather than the builtin print function:Matlab's default toolbar Print […]

#9 Comment By Akshay Kumar On April 30, 2018 @ 23:15

Hi,

My problem is: after clicking on brushing tool, I select data by dragging over the data-points. Now, when I right click it gives me a 5 to 6 options: remove data, replace with , delete create variables etc. Can I customize these options and create callbacks for these options? because I don’t want all these options in my objective.

with regards,
Akshay Kumar


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/modifying-default-toolbar-menubar-actions

URLs in this post:

[1] here: http://undocumentedmatlab.com/blog/figure-toolbar-components/

[2] here: http://undocumentedmatlab.com/blog/figure-toolbar-customizations/

[3] comments section: #respond

[4] Customizing the standard figure toolbar, menubar : https://undocumentedmatlab.com/articles/customizing-standard-figure-toolbar-menubar

[5] Customizing figure toolbar background : https://undocumentedmatlab.com/articles/customizing-figure-toolbar-background

[6] Toolbar button labels : https://undocumentedmatlab.com/articles/toolbar-button-labels

[7] Getting default HG property values : https://undocumentedmatlab.com/articles/getting-default-hg-property-values

[8] Handle object as default class property value : https://undocumentedmatlab.com/articles/handle-object-as-default-class-property-value

[9] uicontrol side-effect: removing figure toolbar : https://undocumentedmatlab.com/articles/uicontrol-side-effect-removing-figure-toolbar

[10] : https://undocumentedmatlab.com/blog/customizing-print-setup/

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.