zhouyang664 发表于 2010-8-25 09:09

基于.m文件(not guide)的文件菜单的编写

%菜单的创建和使用
%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(),''r'')');
hmEsub22=uimenu(hmEsub2,'label','Cos', 'callback','plot(cos(),''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);

paradiseboy 发表于 2010-8-29 13:49

呵呵,我喜欢用这种模式做GUI。
谢谢分享。
我在MATLAB2009b中运行,有些警告。

penghust 发表于 2010-8-30 11:02

最近也在学习中,先给你顶一下吧:handshake

zhouyang664 发表于 2010-8-31 22:21

回复 paradiseboy 的帖子


    m文件形式的gui代码紧凑,还能实现guide不能实现的一些功能,但比较大一点的gui,编写代码就比较麻烦了;

ChaChing 发表于 2010-9-1 01:06

试了下的确有些警告, 应该尚有精进的地方!

aspen 发表于 2010-9-1 04:56

个人推荐一下,欢迎大家积极分享自己的经验

zhouyang664 发表于 2010-9-7 22:31

回复 ChaChing 的帖子


   这个文件是我在学习过程不断添加进去的,介意学习的同志们进入debugger模式或者是cell模式一段一段的执行!(执行前面一段可以将后面的注释掉,Ctrl+R或者是text-->comment就可以了!

qibbxxt 发表于 2010-9-9 15:21

相对来讲,还是编码的方式实现的功能要多一些,不过gui的方式比较简单好操作,关于这一方面可以看一些游戏的程序,尤其推荐初学者看看“萝卜驿站”的程序,对编码的方法的学习大有裨益

88484532 发表于 2011-10-22 14:41

好资料,学习了

matlab-vib 发表于 2011-10-27 15:26

学习中。。.
页: [1]
查看完整版本: 基于.m文件(not guide)的文件菜单的编写