So, I have a legend (on a plot) in an application I'm working on. If you right click on it a bunch of optional actions come up. These include things like 'Interpreter', 'Location', 'Orientation', etc.. I know that one can overwrite this menu by setting your own uicontextmenu set(axes,'uicontextmenu',newmenu)
, but how do you just edit it? What if I wanted to prevent a user from adjusting the legend's location but nothing else?
Is this kind of customisation possible? Here's the code I've been testing/messing with this on.
x = 1:20;
y = cos(x);
z = sin(x);
plot(x,y);
hold on
plot(x,z);
lg = legend('stuff1','stuff2');
% remove the menu altogether
%set(lg,'uicontextmenu','')
I am running R2014b
EDIT: To be completely clear, I want to be able to remove some options from an existing uicontextmenu (that I did not create explicitly), but not all.
The first thing you'll need to do is set the ShowHiddenHandles
property of the root object to 'on'
, which will make hidden handles discoverable. Then you can just do the following:
>> hMenu = get(lg, 'UIContextMenu') % Get the context menu handle
hMenu =
ContextMenu with properties:
Callback: ''
Children: [12×1 Menu] % This would be empty if handles were still hidden
Show all properties
>> hItems = get(hMenu, 'Children') % Get the menu item handles
hItems =
12×1 Menu array:
Menu (scribe:legend:mcode)
Menu (scribe:legend:propedit)
Menu (scribe:legend:orientation)
Menu (scribe:legend:location)
Menu (scribe:legend:interpreter)
Menu (scribe:legend:font)
Menu (scribe:legend:linewidth)
Menu (scribe:legend:edgecolor)
Menu (scribe:legend:color)
Menu (scribe:legend:edittitle)
Menu (scribe:legend:delete)
Menu (scribe:legend:refresh)
>> delete(hItems(4)); % Delete the fourth item
The above could also be done with dot notation for property access like so:
delete(lg.UIContextMenu.Children(4));
In addition, you can leave the handles hidden and use findall
, which requires you to know some of the properties of the object you're looking for. For example, to find and delete the menu object with the 'Label'
property set to 'Location'
in the current figure, do this:
delete(findall(gcf, 'Label', 'Location'));
And for all of the above you can confirm that the "Location" option is now gone from the context menu: