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(我没有明确创建)中删除一些选项,但不是全部.
你需要做的第一件事是设置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)
对于上述所有情况,您可以确认"位置"选项现在已从上下文菜单中消失: