MATLAB: How to define custom context menu for a ROI object

Image Processing Toolbox

Functions such as imellipse or imrect come with their own ready-made context menus. 
How can I override those and define my own custom context menus for ROI objects?

Best Answer

You need to set the uicontextmenu property of the underlying patch and line objects
which constitute the ROI object. Code example:
 
figure
I=imshow('pout.tif');
% create a custom context menu
cmHandle = uicontextmenu;
uimenu(cmHandle,'Label','First Item');
uimenu(cmHandle,'Label','Second Item');
h = imellipse;
% set all context menus for the underlying line and patch objects
cmenu_obj = findobj(gcf,'Type','line','-or','Type','patch');
set(cmenu_obj,'uicontextmenu',cmHandle);