MATLAB: How to override the zoom uicontextmenu with a custom uicontextmenu, when in zoom mode, in MATLAB 7.3 (R2006b)

MATLABmodeuicontextmenuzoom

When I use the command "zoom on", I would like to completely remove the default "uicontextmenu" (that shows zoom out, Reset to original view, etc.) and to replace it with a custom menu.

Best Answer

This can be accomplished with the zoom accessor object. The following example illustartes how this can be done:
 
% Define the context menu
cmenu = uicontextmenu;
% Define the line and associate it with the context menu
hline = plot(1:10,'UIContextMenu', cmenu);
% Define callbacks for context menu items
cb1 = ['set(hline, ''LineStyle'', ''--'')'];
cb2 = ['set(hline, ''LineStyle'', '':'')'];
cb3 = ['set(hline, ''LineStyle'', ''-'')'];
% Define the context menu items
item1 = uimenu(cmenu, 'Label', 'dashed', 'Callback', cb1);
item2 = uimenu(cmenu, 'Label', 'dotted', 'Callback', cb2);
item3 = uimenu(cmenu, 'Label', 'solid', 'Callback', cb3);
% Get the handle to the zoom mode object for the figure
zh = zoom(gcf);
% Set the UI Context Menu to the custom menu
set(zh, 'uicontextmenu', cmenu)