如何删除 MATLAB 中的特定 uicontextmenu 选项(例如,在图例的菜单中)

How do I remove specific uicontextmenu options in MATLAB (e.g. in the legend's menu)

所以,我正在处理的应用程序中有一个图例(在图上)。如果您右键单击它,则会出现一系列可选操作。这些包括 'Interpreter'、'Location'、'Orientation' 等内容。我知道可以通过设置自己的 uicontextmenu set(axes,'uicontextmenu',newmenu) 来覆盖此菜单,但是您如何编辑它?如果我只想阻止用户调整图例的位置怎么办?

这种定制可以吗? 这是我 testing/messing 使用此功能的代码。

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

我是运行 R2014b

编辑:为了完全清楚,我希望能够从现有的 uicontextmenu(我没有明确创建)中删除一些选项,但不是全部。

您需要做的第一件事是将 ShowHiddenHandles property of the root object 设置为 'on',这将使隐藏句柄可被发现。然后您可以执行以下操作:

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

上面的内容也可以通过 属性 访问的点符号来完成,如下所示:

delete(lg.UIContextMenu.Children(4));

此外,您可以隐藏句柄并使用 findall,这需要您了解您要查找的对象的一些属性。例如,要查找并删除当前图中 'Label' 属性 设置为 'Location' 的菜单对象,请执行以下操作:

delete(findall(gcf, 'Label', 'Location'));

对于以上所有内容,您可以确认 "Location" 选项现在已从上下文菜单中消失: