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

Modifying default toolbar/menubar actions

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});

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...)

% 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...'));

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');

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 and here). 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 below.

Related posts:

  1. Customizing the standard figure toolbar, menubar – The standard figure toolbar and menubar can easily be modified to include a list of recently-used files....
  2. Customizing figure toolbar background – Setting the figure toolbar's background color can easily be done using just a tiny bit of Java magic powder. This article explains how. ...
  3. Toolbar button labels – GUI toolbar button labels can easily be set and customized using underlying Java components. ...
  4. Getting default HG property values – Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...
  5. Handle object as default class property value – MCOS property initialization has a documented but unexpected behavior that could cause many bugs in user code. ...
  6. uicontrol side-effect: removing figure toolbar – Matlab's built-in uicontrol function has a side-effect of removing the figure toolbar. This was undocumented until lately. This article describes the side-effect behavior and how to fix it....
Handle graphics Menubar Pure Matlab Toolbar Undocumented feature
Print Print
« Previous
Next »
9 Responses
  1. lux March 15, 2011 at 10:25 Reply

    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.

    • Yair Altman March 16, 2011 at 05:57 Reply

      @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 http://undocumentedmatlab.com/blog/customizing-print-setup/, which explains how to programmatically modify the default print setup.

  2. Andi June 29, 2011 at 10:32 Reply

    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

    • Yair Altman June 29, 2011 at 14:49 Reply

      @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);

      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.

  3. klimmbimm July 28, 2011 at 03:59 Reply

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

    • Yair Altman July 28, 2011 at 12:12 Reply

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

  4. Customizing figure toolbar background | Undocumented Matlab March 14, 2013 at 08:43 Reply

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

  5. Another couple of Matlab bugs and workarounds | Undocumented Matlab December 10, 2014 at 15:06 Reply

    […] 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 […]

  6. Akshay Kumar April 30, 2018 at 23:15 Reply

    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

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