您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 第08章_MATLAB图形用户界面设计_例题源程序
高教社刘卫国《MATLAB程序设计与应用》(第二版)例题源程序第8章:MATLAB图形用户界面设计王成优©山东大学(威海)MATLAB语言WangChengyou©ShandongUniversity,Weihai1第8章MATLAB图形用户界面设计例8.1建立图8.1所示的图形演示系统菜单。菜单条中含有3个菜单项:Plot、Option和Quit。Plot中有SineWave和CosineWave两个子菜单项,分别控制在本图形窗口画出正弦和余弦曲线。Option菜单项的内容如图8.1所示,其中Gridon和Gridoff控制给坐标轴加网格线,Boxon和Boxoff控制给坐标轴加边框,而且这4项只有在画有曲线时才是可选的。WindowColor控制图形窗口背景颜色。Quit控制是否退出系统。screen=get(0,'ScreenSize');W=screen(3);H=screen(4);figure('Color',[1,1,1],'Position',[0.2*H,0.2*H,0.5*W,0.3*H],...'Name','图形演示系统','NumberTitle','off','MenuBar','none');%定义Plot菜单项hplot=uimenu(gcf,'Label','&Plot');uimenu(hplot,'Label','SineWave','Call',...['t=-pi:pi/20:pi;','plot(t,sin(t));',...'set(hgon,''Enable'',''on'');',...'set(hgoff,''Enable'',''on'');',...'set(hbon,''Enable'',''on'');',...'set(hboff,''Enable'',''on'');']);uimenu(hplot,'Label','CosineWave','Call',...['t=-pi:pi/20:pi;','plot(t,cos(t));',...'set(hgon,''Enable'',''on'');',...'set(hgoff,''Enable'',''on'');',...'set(hbon,''Enable'',''on'');',...'set(hboff,''Enable'',''on'');']);%定义Option菜单项hoption=uimenu(gcf,'Label','&Option');hgon=uimenu(hoption,'Label','&Gridon',...'Call','gridon','Enable','off');hgoff=uimenu(hoption,'Label','&Gridoff',...'Call','gridoff','Enable','off');hbon=uimenu(hoption,'Label','&Boxon',...高教社刘卫国《MATLAB程序设计与应用》(第二版)例题源程序第8章:MATLAB图形用户界面设计王成优©山东大学(威海)MATLAB语言WangChengyou©ShandongUniversity,Weihai2'separator','on','Call','boxon','Enable','off');hboff=uimenu(hoption,'Label','&Boxoff',...'Call','boxoff','Enable','off');hwincor=uimenu(hoption,'Label','&WindowColor','Separator','on');uimenu(hwincor,'Label','&Red','Accelerator','r',...'Call','set(gcf,''Color'',''r'');');uimenu(hwincor,'Label','&Blue','Accelerator','b',...'Call','set(gcf,''Color'',''b'');');uimenu(hwincor,'Label','&Yellow','Call',...'set(gcf,''Color'',''y'');');uimenu(hwincor,'Label','&White','Call',...'set(gcf,''Color'',''w'');');%定义Quit菜单项uimenu(gcf,'Label','&Quit','Call','close(gcf)');例8.2绘制曲线y=2e-0.5xsin(2πx),并建立一个与之相联系的快捷菜单,用以控制曲线的线型和曲线宽度。x=0:pi/100:2*pi;y=2*exp(-0.5*x).*sin(2*pi*x);hl=plot(x,y);hc=uicontextmenu;%建立快捷菜单hls=uimenu(hc,'Label','线型');%建立菜单项hlw=uimenu(hc,'Label','线宽');uimenu(hls,'Label','虚线','Call','set(hl,''LineStyle'','':'');');uimenu(hls,'Label','实线','Call','set(hl,''LineStyle'',''-'');');uimenu(hlw,'Label','加宽','Call','set(hl,''LineWidth'',2);');uimenu(hlw,'Label','变细','Call','set(hl,''LineWidth'',0.5);');set(hl,'UIContextMenu',hc);%将该快捷菜单和曲线对象联系起来例8.3建立如图8.10所示的数制转换对话框。在左边输入一个十进制整数和2~16之间的数,单击“转换”按钮能在右边得到十进制数所对应的2~16进制字符串,单击“退出”按钮退出对话框。hf=figure('Color',[0,1,1],'Position',[100,200,400,200],...'Name','数制转换','NumberTitle','off','MenuBar','none');高教社刘卫国《MATLAB程序设计与应用》(第二版)例题源程序第8章:MATLAB图形用户界面设计王成优©山东大学(威海)MATLAB语言WangChengyou©ShandongUniversity,Weihai3uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.05,0.8,0.45,0.1],'Horizontal','center',...'String','输入框','Back',[0,1,1]);uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.5,0.8,0.45,0.1],'Horizontal','center',...'String','输出框','Back',[0,1,1]);uicontrol(hf,'Style','Frame','Units','normalized',...'Position',[0.04,0.33,0.45,0.45],'Back',[1,1,0]);uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.05,0.6,0.25,0.1],'Horizontal','center',...'String','十进制数','Back',[1,1,0]);uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.05,0.4,0.25,0.1],'Horizontal','center',...'String','2~16进制','Back',[1,1,0]);he1=uicontrol(hf,'Style','Edit','Units','normalized',...'Position',[0.25,0.6,0.2,0.1],'Back',[0,1,0]);he2=uicontrol(hf,'Style','Edit','Units','normalized',...'Position',[0.25,0.4,0.2,0.1],'Back',[0,1,0]);uicontrol(hf,'Style','Frame','Units','normalized',...'Position',[0.52,0.33,0.45,0.45],'Back',[1,1,0]);ht=uicontrol(hf,'Style','Text','Units','normalized',...'Position',[0.6,0.5,0.3,0.1],'Horizontal','center',...'Back',[0,1,0]);COMM=['n=str2num(get(he1,''String''));',...'b=str2num(get(he2,''String''));',...'dec=trdec(n,b);','set(ht,''string'',dec);'];uicontrol(hf,'Style','Push','Units','normalized',...'Position',[0.18,0.1,0.2,0.12],'String','转换','Call',COMM);uicontrol(hf,'Style','Push','Units','normalized',...'Position',[0.65,0.1,0.2,0.12],...'String','退出','Call','close(hf)');程序调用了trdec.m函数文件,该函数的作用是将任意十进制整数转换为2~16进制字高教社刘卫国《MATLAB程序设计与应用》(第二版)例题源程序第8章:MATLAB图形用户界面设计王成优©山东大学(威海)MATLAB语言WangChengyou©ShandongUniversity,Weihai4符串。trdec.m函数文件如下:functiondec=trdec(n,b)ch1='0123456789ABCDEF';%十六进制的16个符号k=1;whilen~=0%不断除某进制基数取余直到商为0p(k)=rem(n,b);n=fix(n/b);k=k+1;endk=k-1;strdec='';whilek=1%形成某进制数的字符串kb=p(k);strdec=strcat(strdec,ch1(kb+1:kb+1));k=k-1;enddec=strdec;例8.4建立如图8.11所示的图形演示对话框。在编辑框输入绘图命令,当单击“绘图”按钮时,能在左边坐标轴绘制所对应的图形,弹出框提供色图控制,列表框提供坐标网格线和坐标边框控制。clf;set(gcf,'Unit','normalized','Position',[0.2,0.3,0.55,0.36]);set(gcf,'Menubar','none','Name','图形演示','NumberTitle','off');axes('Position',[0.05,0.15,0.55,0.7]);uicontrol(gcf,'Style','text','Unit','normalized',...'Posi',[0.63,0.85,0.2,0.1],'String',...'输入绘图命令','Horizontal','center');hedit=uicontrol(gcf,'Style','edit','Unit','normalized',...'Posi',[0.63,0.15,0.2,0
本文标题:第08章_MATLAB图形用户界面设计_例题源程序
链接地址:https://www.777doc.com/doc-2169376 .html