马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
- %菜单的创建和使用
- %uimenu最重要的属性是label和callback。label属性值是菜单条和下拉菜单项的文本字符串。
- %callback属性值是MATLAB字符串,为调用函数,其作用是当选中菜单项时传给eval用以执行
- %
- hmE=uimenu(gcf,'label','Exa&mple'); %Example为顶部菜单,默认放在菜单栏最后
- hmEsub1=uimenu(hmE,'label','Grid','callback','Grid');%callback的值是字符串
- hmEsub2=uimenu(hmE,'label','View');
- hmEsub21=uimenu(hmEsub2,'label','Sin', 'callback','plot(sin([0:0.05*pi:2*pi]),''r'')');
- hmEsub22=uimenu(hmEsub2,'label','Cos', 'callback','plot(cos([0:0.05*pi:2*pi]),''b'')');
- %分隔线Separator
- hmEsub3=uimenu(hmE,'label','Hold','Callback','hold','separator','on');
- hmEsub4=uimenu(hmE,'label','Grid On','callback','grid on;set(gca,''box'',''on'')');
- %hmEsub4.label='Grid On';hmEsub4.callback=['grid on;','set(gca,''box'',''on'')'];uimenu(hmE,hmEsub4);
- hmC=uimenu(gcf,'label','Close');
- hmCsub1=uimenu(hmC,'label','Remove Example Menu','callback','delete(hmE);drawnow');
- hmCsub2=uimenu(hmC,'label','Close Figure', 'callback','close');
- %菜单快捷键
- hmD=uimenu(gcf,'label','&Color');
- hmDsub1=uimenu(hmD,'label','&Blue', 'Callback','set(gcf,''color'',''blue'')', 'Accelerator','b');
- %菜单的外观
- set(gcf,'menubar','none'); %set(gcf,'menubar','figure')
- %位置Position
- hmO=uimenu('label','&Option','Position',4);
- %标志Checked
- hmOsub1=uimenu(hmO,'label','grid on', 'callback',[ 'grid on;', ...
- 'set(hmOsub1,''checked'',''on'');', 'set(hmOsub2,''checked'',''off'');']);
- hmOsub2=uimenu(hmO,'label','grid off', 'callback',[ 'grid off;',...
- 'set(hmOsub2,''checked'',''on'');', 'set(hmOsub1,''checked'',''off'');']);
- %可用性Enable与可见性Visible
- option=uimenu('label','Option');
- op_sub1=uimenu(option,'label','axis on'); op_sub2=uimenu(option,'label','axis off','enable','off');
- op_sub3=uimenu(option,'label','grid on','separator','on','visible','off');
- op_sub4=uimenu(option,'label','grid on','visible','off');
- set(op_sub1,'callback',['axis on;', 'set(op_sub1,''enable'',''off'');', 'set(op_sub2,''enable'',''on'');',...
- 'set(op_sub3,''visible'',''on'');', 'set(op_sub4,''visible'',''on'');']);
- set(op_sub2,'callback',['axis off;', 'set(op_sub1,''enable'',''on'');', 'set(op_sub2,''enable'',''off'');',...
- 'set(op_sub3,''visible'',''off'');', 'set(op_sub4,''visible'',''off'');']);
- set(op_sub3,'callback',['grid on;', 'set(op_sub3,''visible'',''off'');', 'set(op_sub4,''visible'',''on'');']);
- set(op_sub4,'callback',['grid off;', 'set(op_sub3,''visible'',''on'');', 'set(op_sub4,''visible'',''off'');']);
- %创建右键弹出菜单
- t=0:0.05*pi:2*pi; y=sin(t)./t;hline=plot(t,y); cm=uicontextmenu;
- uimenu(cm,'label','Red','callback','set(hline,''color'',''r''),');
- uimenu(cm,'label','Green','callback','set(hline,''color'',''g''),');
- uimenu(cm,'label','Blue','callback','set(hline,''color'',''b''),');
- set(hline,'uicontextmenu',cm);
复制代码
|