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

J. *_*ick 4 matlab contextmenu matlab-figure

所以,我在一个正在研究的应用程序中有一个图例(在图上).如果你右键单击它会出现一堆可选操作.这些包括"翻译","位置","方向"等内容.我知道可以通过设置您自己的uicontextmenu来覆盖此菜单set(axes,'uicontextmenu',newmenu),但您如何编辑它?如果我想阻止用户调整图例的位置但没有别的怎么办?

这种定制是否可行?这是我一直在测试/弄乱这个的代码.

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','')
Run Code Online (Sandbox Code Playgroud)

我正在运行R2014b

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

gno*_*ice 5

你需要做的第一件事是设置ShowHiddenHandles属性的的根对象'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
Run Code Online (Sandbox Code Playgroud)

以上也可以使用点符号进行属性访问,如下所示:

delete(lg.UIContextMenu.Children(4));
Run Code Online (Sandbox Code Playgroud)

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

delete(findall(gcf, 'Label', 'Location'));
Run Code Online (Sandbox Code Playgroud)

对于上述所有情况,您可以确认"位置"选项现在已从上下文菜单中消失:

在此输入图像描述

  • 这个隐藏把手的技巧很巧妙,不知道我以前怎么没见过。还有其他立即有用的应用程序吗? (2认同)
  • @Wolfie:如果使用`findall`而不是`findobj`(`findall`忽略隐藏状态),你实际上不必乱用`ShowHiddenHandles`.我构建我的GUI,将句柄可见性设置为隐藏,以使用户不太可能破坏它们. (2认同)