matlab中uimenu,检查时如何防止uimenu(MATLAB)消失

此示例受到Benoit_11解决方案的极大启发,但有所改进.我还给人的印象是,回调中的3个不同函数在做不同的事情,因此我使3个不同的菜单更改了行的不同属性(而不是用不同的值更改相同的属性).

我在一个嵌套函数中做了uimenu回调.它根据uimenu定义提供的参数what2do决定要做什么(但可以保留3个独立的函数).但是,请注意,对所有uimenu切换复选标记的功能都是相同的(您不需要为每个uimenu单独设置功能).

function hf = TestUiContext2

%// Extension of Benoit_11 solution

clear ; clc ; close all

hf = figure ; %// return the handle of the figure

hax = axes; %// Create axes and save handle

plot(rand(20,3)); %// Plot three lines

hcmenu = uicontextmenu; %// Define a context menu; it is not attached to anything

%// Define the context menu items and install their callbacks

item1 = uimenu(hcmenu, 'Label','Bold line' , 'Callback' , {@uiCallback,'bold'} );

item2 = uimenu(hcmenu, 'Label','Dotted line' , 'Callback' , {@uiCallback,'dots'} );

item3 = uimenu(hcmenu, 'Label','Markers on' , 'Callback' , {@uiCallback,'mark'} );

hlines = findall(hax,'Type','line'); %// Locate line objects

for line = 1:length(hlines) %// Attach the context menu to each line

set(hlines(line),'uicontextmenu',hcmenu)

end

function uiCallback(obj,~,what2do)

hline = gco ;

switch what2do

case 'bold'

toggle_bold_line(hline)

case 'dots'

toggle_dotted_line(hline)

case 'mark'

toggle_markers(hline)

end

%// reposition the context menu and make it visible

set(hcmenu,'Position',get(gcf,'CurrentPoint'),'Visible','on')

toggle_checkmark(obj) %// toggle the checkmark

end

function toggle_checkmark(obj)

if strcmp(get(obj,'Checked'),'on')

set(obj,'Checked','off')

else

set(obj,'Checked','on')

end

end

function toggle_bold_line(hline)

if get(hline,'LineWidth')==0.5

set(hline,'LineWidth',2)

else

set(hline,'LineWidth',0.5)

end

end

function toggle_dotted_line(hline)

if strcmpi(get(hline,'LineStyle'),':')

set(hline,'LineStyle','-')

else

set(hline,'LineStyle',':')

end

end

function toggle_markers(hline)

if strcmpi(get(hline,'Marker'),'none')

set(hline,'Marker','o')

else

set(hline,'Marker','none')

end

end

end

现在您可以一口气勾选所有菜单了;)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值