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

如何在MATLAB中删除特定的uicontextmenu选项(例如,在图例和菜单中),matlab,contextmenu,matlab-figure,Matlab,Contextmenu,Matlab Figure,所以,我正在开发的应用程序中有一个图例(在绘图上)。如果你右键点击它,就会出现一系列可选的操作。这些包括“口译员”、“位置”、“方向”等。。我知道你可以通过设置你自己的uicontextmenuset(轴,'uicontextmenu',newmenu)来覆盖这个菜单,但是你怎么编辑它呢?如果我只想阻止用户调整图例的位置,而不做其他事情,该怎么办 这种定制可能吗? 这是我一直在测试的代码 x = 1:20; y = cos(x); z = sin(x); plot(x,y); hold on p

所以,我正在开发的应用程序中有一个图例(在绘图上)。如果你右键点击它,就会出现一系列可选的操作。这些包括“口译员”、“位置”、“方向”等。。我知道你可以通过设置你自己的uicontextmenu
set(轴,'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','')
我正在运行R2014b


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

您需要做的第一件事是将的设置为
'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));
此外,您可以将句柄隐藏并使用,这要求您了解正在查找的对象的某些属性。例如,要查找和删除当前图形中
'Label'
属性设置为
'Location'
的菜单对象,请执行以下操作:

delete(findall(gcf, 'Label', 'Location'));
对于以上所有内容,您可以确认“位置”选项现在已从关联菜单中消失:


您是否尝试过为图例设置固定位置?我认为这会阻止进一步的手动移动……这只是一个例子,我并不是特别想阻止图例定位;我不想让用户使用这个选项。这个隐藏手柄的把戏很巧妙,我不知道我以前怎么没见过。还有其他立即有用的应用程序吗?@Wolfie:如果您使用
findall
而不是
findobj
findall
忽略隐藏状态),您实际上不必处理
ShowHiddenHandles
)。我构建GUI时将句柄可见性设置为hidden,以减少用户破坏它们的可能性。