1 / 36

MATLAB 程序设计基础

MATLAB 程序设计基础. 图形用户界面编程. MATLAB 图形界面中各对象关系. 线对象 line. 右键菜单对象 uicontextmenu. 文字对象 text. 菜单对象 uimenu. 根对象 root. 图形窗口对象 figure. 表面图对象 surface. 控件对象 uicontrol. 图像对象 image. 坐标轴对象 axes. 光源对象 light. 窗口对象及其属性设定. 窗口对象的建立 hwin=figure( 属性 1 ,属性值 1 ,属性 2 ,属性值 2 , …)

melchior
Download Presentation

MATLAB 程序设计基础

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB程序设计基础 图形用户界面编程

  2. MATLAB图形界面中各对象关系 线对象 line 右键菜单对象 uicontextmenu 文字对象 text 菜单对象 uimenu 根对象 root 图形窗口对象 figure 表面图对象 surface 控件对象 uicontrol 图像对象 image 坐标轴对象 axes 光源对象 light

  3. 窗口对象及其属性设定 • 窗口对象的建立 hwin=figure(属性1,属性值1,属性2,属性值2,…) hwin为图形窗口的句柄,matlab环境允许打开多个窗口,每个窗口都对应自己的句柄,通过该句柄可以进一步对窗口的属性等进行操作。 hwin=gcf 获得当前窗口的句柄。

  4. 图形窗口的属性

  5. 常用属性 color属性 由红绿蓝三原色组成,取值范围为0到1。 menubar属性 设置菜单条的形式。 name属性 设置图形窗口的标题栏中标题内容,其属性值为一字符串。 numbertitle属性 是否显示图形窗口标题栏中的图形标号。 units属性 设定图形单位。如:pixels(象素点)、inches(英寸)、centimeters(厘米)、normalized(归一值)等

  6. position属性 设定窗口的位置和大小,为1×4向量,前两个值为窗口左下角横纵坐标值,后两个元素值为窗口的宽度和高度,其单位由units属性设定。 resize属性 确定是否可以改变图形窗口的大小。 toolbar属性 是否显示工具条。 visible属性 决定窗口是否为可见。 Pointer属性 设置鼠标的显示形式。

  7. 对象属性的修改 • 改变对象的属性值 set(对象句柄,属性1,属性值1,属性2,属性值2,…) 属性的名称用单引号括起来;如果不给出属性值,则返回全部允许的属性值。 • 获得对象的属性值 V=get(句柄名,属性) ?gwin=figure('visible','off'); ?set(gwin,'color',[1,0,0],'position',[100,200,300,300],... 'name','My program','numbertitle','off','menubar','none'); ?set(gwin,'visible','on')

  8. 回调函数(callback function) • CloseRequestFcn-关闭窗口时响应函数 • KeyPressFcn-键盘按下时响应函数 • windowButtonDownFcn-鼠标按下时响应函数 • WindowButtonMotionFcn-鼠标移动时响应函数 • CreateFcn和DeleteFcn-建立和删除对象时响应函数 • CallBack-对象被选中时响应函数

  9. 一旦该对象指定的事件发生,将自动调用某指定的函数,它可以是matlab文件,也可以是一组matlab程序。一旦该对象指定的事件发生,将自动调用某指定的函数,它可以是matlab文件,也可以是一组matlab程序。 ?gwin=figure gwin = 1 ?set(gwin,'windowbuttondownfcn','helpdlg(''mouse button down!'')') ?set(gwin,'keypressfcn','helpdlg(''keyboard pressed!'')')

  10. 标准对话框及其调用 • 文件名操作函数 uigetfile()和uiputfile()函数打开一个文件进行读、写的对话框。 [fname,pname]=uigetfile(ffilter,strtitle,x,y) ffilter为文件名过滤器;strtiltle为对话框窗口标题栏的显示内容;x、y为对话框出现的位置,省略则采用默认位置。

  11. ?[f,p]=uigetfile('*.m;*.txt','请选择一个文件') f = fileprint.m p = E:\MATLABR11\work\

  12. 颜色设置对话框 c=uisetcolor; 或 c=uisetcolor(c0); 函数返回一个1×3的颜色向量,分别对应红、绿、蓝三原色,按下“取消”按钮后返回空的向量;如果给出向量c0,则在图中指向c0所定义的颜色位置,且按下“取消”按钮时返回c0的值。

  13. 字体设置对话框 h_Font=uisetfont 或 h_Font=uisetfont(h_Text,strTitle) h_Font为字体属性的结构体;h_Text为要设置的字符句柄,strTitle为对话框的标题栏内容。 ?s=uisetfont s = FontName: '楷体_GB2312' FontUnits: 'points' FontSize: 42 FontWeight: 'bold' FontAngle: 'normal'

  14. 警告与错误信息对话框 warndlg和 errordlg函数 二者的显示图标不同。 例: ?h=warndlg({‘error:’,‘code 1111.’},‘Warning’) ?h=errordlg({'error:','code 1111.'},'Error')

  15. 帮助信息对话框 与警告、错误信息对话框基本一致,仅仅是图标的不同。 例: ?h=helpdlg({'帮助信息:','帮助信息对话框和警告错误对话框基本一致,只是图标不同!'},'帮助')

  16. 通用信息框 msgbox(‘显示信息’,’标题’,’图标’) 图标包括:Error、Help、Warn以及Custom,如果缺省则为None。 ?data=1:64;data=(data'*data)/64; ?msgbox('This is an example of msgbox!','custom ico','custom',data,hot(64))

  17. Matlab图形界面基本控件 • 静态文本’text’ 一般用来作为信息提示用。 • 编辑框’edit’ 读取用户数据的文字窗口。 • 框架 ‘frame’ 概括一组控件,也可以修饰用。 • 列表框‘list’ 包含多个选项供用户选择。

  18. 滚动条’slider’ 图示某个范围。 • 按钮’pushbutton’ 其上的字符说明其作用。 • 切换按钮’toggle’ 两状态按钮,单击改变其状态。 • 收音机按钮’radio’ 一组选项,其中只能有一个被选中。 • 检取框’check’ 一组选项,可以选择多项。 • 弹出式菜单’popup’ 选中时打开一个列表。

  19. 控件的常用属性 • Units与position属性 意义同上,这里的位置是相对该窗口的左下角; • String属性 标注在控件上,用来说明或提示; • Callback属性 实现该控件的实质性功能; • Enable属性 该控件的使能状态‘on’或‘off’

  20. Cdata属性 真彩色位图,为一三维数组,用于美化界面; • Tooltipstring属性 鼠标指针位于该控件时的提示信息显示; • Interruptable属性 是否中断当前回调函数的执行,‘on’或‘off’ • 字体属性 设置字体

  21. h_main=figure(‘name’,‘a demo of gui design’,‘menubar’,‘none’,… 'numbertitle','off','position',[100 100 300 100]); h_edit=uicontrol('style','edit','backgroundcolor',[1 1 1],'position',[20 20 50 20],... 'tag','myedit','string','1','horizontalalignment','left'); h_but1=uicontrol('style','pushbutton','position',[20 50 50 20],'string','INC',... 'callback',['v=eval(get(h_edit,''string''));',... 'set(h_edit,''string'',int2str(v+1));']); h_but2=uicontrol('style','pushbutton','position',[80 50 50 20],'string','DEC',... 'callback',['v=eval(get(h_edit,''string''));','set(h_edit,''string'',int2str(v-1));']); • 例1:

  22. function gui_counter1() %gui_counter is a demo of gui design. h_main=figure('name','a demo of gui design','menubar','none',... 'numbertitle','off','position',[100 100 300 100]); h_edit=uicontrol('style','edit','backgroundcolor',[1 1 1],'position',[20 20 50 20],... 'tag','myedit','string','1','horizontalalignment','left'); h_but1=uicontrol('style','pushbutton','position',[20 50 50 20],'string','INC',... 'callback',['h=findobj(gcf,''tag'',''myedit'');','v=eval(get(h,''string''));',... 'set(h,''string'',int2str(v+1));']); h_but2=uicontrol('style','pushbutton','position',[80 50 50 20],'string','DEC',... 'callback',['h=findobj(gcf,''tag'',''myedit'');','v=eval(get(h,''string''));',... 'set(h,''string'',int2str(v-1));']);

  23. function gui_demo() %GUI_demo is another demo of GUI design. h_main=figure('units','normalized','position',[0.3 0.3 0.5 0.4],... 'name','GUI demostration','numbertitle','off'); h_axis=axes('units','normalized','position',[0.3 0.15 0.6 0.7],... 'tag','axplot','xlim',[0 10],'ylim',[-1 1]); t=0:0.1:10;y=sin(t);line(t,y); bmp1=imread('1.bmp');bmp2=imread('2.bmp'); bmp3=imread('3.bmp');bmp4=imread('4.bmp'); h_1=uicontrol('style','pushbutton','units','normalized',... 'position',[0.1 0.6 0.06 0.1],'cdata',bmp1,... 'callback','zoom on','tooltipstring','Enable zooming'); h_2=uicontrol('style','pushbutton','units','normalized',... 'position',[0.04 0.45 0.06 0.1],'cdata',bmp2,... 'callback','zoom xon','tooltipstring','Enable zoom on x-axis only'); h_3=uicontrol('style','pushbutton','units','normalized',... 'position',[0.16 0.45 0.06 0.1],'cdata',bmp3,... 'callback','zoom yon','tooltipstring','Enable zoom on y-axis only'); h_4=uicontrol('style','pushbutton','units','normalized',... 'position',[0.1 0.3 0.06 0.1],'cdata',bmp4,... 'callback','zoom off','tooltipstring','Disable zooming'); • 例2

  24. Matlab菜单系统设计 • 菜单系统的生成 菜单项句柄=uimenu(窗口句柄,属性1,属性值1,属性2,属性值2,…) 子菜单句柄=uimenu(菜单项句柄,属性1,属性值1,…) 属性: • 菜单条名称label • 回调函数callback

  25. 热键名称accelerator • 背景颜色backgroundcolor • 前景颜色foregroundcolor • 选中状态checked • 使能状态enabled • 菜单条位置position • 分隔符separator

  26. ctxmenu=uicontextmenu; set(gcf,'uicontextmenu',ctxmenu); uimenu(ctxmenu,'label','zoom on','callback','zoom on'); uimenu(ctxmenu,'label','x-axis zoom on','callback','zoom xon'); uimenu(ctxmenu,'label','y-axis zoom on','callback','zoom yon'); uimenu(ctxmenu,'label','zoom off','callback','zoom off'); uimenu(ctxmenu,'label','checked','checked','on','separator','on'); uimenu(ctxmenu,'label','disabled','enable','off');

  27. GUI设计工具简介 • guide GUI向导设计器。

  28. 属性设计器(Property Editor) • 设置所选图形对象或GUI控件各属性的值

  29. 控件布局编辑器(Alignment Tool) • 设置控件的布局

  30. 菜单编辑器(Menu Editor) • 编辑菜单项

  31. 回调函数编辑器(Callback Editor) • 编辑控件属性所对应的回调函数

  32. 例:示意小球碰撞过程 上挡板 出射角 下挡板

  33. Tag=rdradio1 Tag=rdradio2 Tag=rdradio3 Tag=lstcolor h1 = line([0,1],-0.09*[1 1]); set(h1,'linewidth',5); h2 = line([0,1],1.09*[1 1]); set(h2,'linewidth',7); uu={[0 1 0], 0.05 0.5}; set(h_main,'userdata',uu);

  34. set(h_mark,'color',uu{1}); for i=1:length(xx_l) x0=xx_l(i); y0=yy_l(i); set(h_mark,‘xdata’,x0,‘ydata’,… y0);pause(uu{2}); end end function exec_bouncing(x) k=1;uu=get(gcf,'userdata'); if nargin==0, [x,y,k]=ginput(1);uu{3}=x;set(gcf,'userdata',uu); end if k==1 if x<0, x=0.1;end if x>1, x=1;end xx=[0];yy=[1];x0=0;x1=x;y1=1; while(x1<=1+x) xx=[xx x1];y1=~y1;yy=[yy y1];x0=x1;x1=x1+x; end dx=1/50;xx_l=[0:dx:1,xx];xx_l=sort(xx_l); yy_l=interp1(xx,yy,xx_l); h_mark=line([0],[1]); set(h_mark,'Marker','o','markersize',8,'color''g',... 'tag','h_ball','linewidth',5);

  35. function set_speed(key) uu=get(gcf,'userdata'); switch key case 1 uu{2}=uu{2}*0.5; case 2 uu{2}=uu{2}*2; case 3 uu{2}=0.05; case 4 kk=get(findobj(gcf,'tag','lstcolor'),'value'); switch kk case 1 uu{1}=[1 0 0]; case 2 uu{1}=[0 1 0]; case 3 uu{1}=[0 0 1]; end end if key<=3 hh=gco; h1=findobj(gcf,'tag','rdradio1');set(h1,'value',0); h2=findobj(gcf,'tag','rdradio2');set(h2,'value',0); h3=findobj(gcf,'tag','rdradio3');set(h3,'value',0); set(hh,'value',1); end set(gcf,'userdata',uu); exec_bouncing(uu{3});

More Related