【*例11.5.3-2】目标:利用M函数文件创建与例11.4.3.3-1相同的用户界面。本例演示:如何依靠 'Tag'属性 与findobj指令的配合使用获取回调操作所必须的控件图柄,保证回调动作正确执行。 本例的程序可由exm1153_1.m做如下修改而得: (1)删去exm1153_1.m的指令<34><36>。 (2)在exm1153_1.m的<20>和<21>行之间增添一行 'Tag','H_edit',... (3)在exm1153_1.m的<23>和<24>行之间增添一行 'Tag','H_popup',... (4)在exm1153_1.m的<27>和<28>行之间增添一行 'Tag','H_list',... (5)把exm1153_1.m的<31><32><33>条指令中的exm1153_1改为exm1153_2。 (6)在exm1153_1.m的<35>和<37>行之间增添以下三条指令。 H(1)=findobj(gcf,'Tag','H_edit'); H(2)=findobj(gcf,'Tag','H_popup'); H(3)=findobj(gcf,'Tag','H_list'); (7)把exm1153_1.m的函数头修改为 function exm1153_2(flag) (8)把修改后的文件“另存为”exm1153_2.m ,就完成了新文件的编写。 (9)在MATLAB指令窗中运行exm1153_2就可获得题目所要求的图形用户界面。 11.6 图形用户界面设计工具 图11.6-1 11.6.1 界面设计工具的结构和调用指令 11.6.1.1 界面设计工具的结构 11.6.1.2 图形窗的激活态和受控态 11.6.1.3 启动交互式编辑工具的指令 11.6.2 交互式用户界面设计工具应用示例 图11.6.2-1 待制作的用户界面 11.6.2.1 工序一:窗口初始位置和大小设计 【例11.6.2.1-1】本例演示:界面设计工具guide 的启动和用户界面窗口初试几何制作。 图11.6.2.1-1 属性编辑工具界面
11.6.2.2 工序二:对象的几何布局 【例11.6.2.2-1】整个用户界面的几何布局:“轴”、控件种类、相对位置及大致尺寸。本例演示:(A)设计工具控制面板上“新对象模块区”的图标的使用;(B)几何布局时不必太多考虑各对象的精细位置和大小。 图 11.6.2.2-1 “第二道工序” 构成控件布局的界面 11.6.2.3 工序三:新建对象的属性设置 【例11.6.2.3-1】控件关键属性的设置。本例演示:(A)属性编辑工具的使用。(B)当 'Callback' 属性值可用比较简单的MATLAB语句表达时,则直接填写;如果语句较多,表达复杂,那么就应采用一个待写的M函数名填写。本例中的回调都借助M函数文件实现。(C)当控件上有字符串标识时,应注意文字的对齐方式和注意字体大小,使外观上与对象大小协调。(D)控件的 'String' 属性字符串的输入格式。在这过程中,可能还要适当调整对象几何尺寸,使字符表现清晰醒目。(E) 'Units' 采用 'normalized' ,使得所有新建对象随所在图形窗按比例缩放。 11.6.2.4 工序四:用户菜单的制作 用户菜单制作工序比较独立,因此该工序可前可后,也可以与“工序一”相合并。 【例11.6.2.4-1】用户菜单的制作。本例演示:菜单编辑工具的使用。在本例中菜单引起的回调都是直接、简单的MATLAB语句。 图 11.6.2.4-1 用户菜单编辑器界面 11.6.2.5 工序五:新建图形对象的齐整化 【例11.6.2.5-1】控件的齐整化。本例演示:演示“对齐编辑工具”的使用。 图11.6.2.5-1
11.6.2.6 工序六:回调函数的编写 【例11.6.2.6-1】回调函数的编写。本例演示:从处理方便出发编写回调函数。(关于回调函数的详细讨论,请看第11.5节的三个算例) (1)弹出框的回调函数Mycolormap.m [Mycolormap.m] function Mycolormap popstr={'spring','summer','autumn','winter'}; %弹出框色图矩阵 vpop=get(findobj(gcf,'Tag','PopupMenu1'),'value'); %获得选项的位置标识 colormap(eval(popstr{vpop})) %采用弹出框所选色图
(2)列表框和“Apply”按键配合的回调函数Myapply.m [Myapply.m] function Myapply vlist=get(findobj(gcf,'Tag','Listbox1'),'value'); %获得选项位置向量 liststr={'grid on','box on','hidden off','axis off'}; %列表框选项内容 invstr={'grid off','box off','hidden on','axis on'}; %列表框的逆指令 vv=zeros(1,4);vv(vlist)=1; for k=1:4 if vv(k);eval(liststr{k});else eval(invstr{k});end %按列表选项影响图形 end
(3)动态编辑框的回调函数Myedit.m [Myedit.m] function Myedit ct=get(findobj(gcf,'Tag','EditText1'),'string'); eval(ct')
11.6.2.7 工序七:界面功能的全面测试 11.6.2.8 为读者提供的配套文件和数据 (1)机器自动生成的主控文件 [Mygui1.m] function fig = Mygui1() % This is the machine-generated representation of a Handle Graphics object % and its children. Note that handle values may change when these objects % are re-created. This may cause problems with any callbacks written to % depend on the value of the handle at the time the object was saved. % This problem is solved by saving the output as a FIG-file.% % To reopen this object, just type the name of the M-file at the MATLAB % prompt. The M-file and its associated MAT-file must be on your path.% % NOTE: certain newer features in MATLAB may not have been saved in this % M-file due to limitations of this format, which has been superseded by % FIG-files. Figures which have been annotated using the plot editor tools % are incompatible with the M-file/MAT-file format, and should be saved as % FIG-files. load Mygui1 h0 = figure('Units','normalized', ... 'Color',[0.8553876799929505 0.8553876799929505 0.8553876799929505], ... 'Colormap',mat0, ... 'FileName','F:\99\m5\Mygui1.m', ... 'MenuBar','none', ... 'Name','GUI工具设计的界面', ... 'PaperPosition',[18 180 575.9999999999999 432], ... 'PaperUnits','points', ... 'Position',[0.1484375 0.5291666666666667 0.8 0.35], ... 'Tag','Fig1', ... 'ToolBar','none'); h1 = uimenu('Parent',h0, ... 'Label','Zoom', ... 'Tag','M_Z'); h2 = uimenu('Parent',h1, ... 'Callback','zoom on', ... 'Label','On', ... 'Tag','M_Zon'); h2 = uimenu('Parent',h1, ... 'Callback','zoom off', ... 'Label','Off', ... 'Tag','M_Zoff'); h1 = uicontrol('Parent',h0, ... 'Units','normalized', ... 'BackgroundColor',[0.7529411764706 0.7529411764706 0.7529411764706], ... 'Callback','Mycolormap', ... 'ListboxTop',0, ... 'Position',[0.80859375 0.773809523809524 0.16 0.130952380952381], ... 'String',['spring ';'summer ';'autumn ';'winter '], ... 'Style','popupmenu', ... 'Tag','PopupMenu1', ... 'value',1); h1 = uicontrol('Parent',h0, ... 'Units','normalized', ... 'BackgroundColor',[1 1 1], ... 'Max',2, ... 'Position',[0.80859375 0.327380952380952 0.16 0.398809523809524], ... 'String',['grid on ';'box on ';'hidden off';'axis off '], ... 'Style','listbox', ... 'Tag','Listbox1', ... 'value',1); h1 = uicontrol('Parent',h0, ... 'Units','normalized', ... 'BackgroundColor',[.752941176470588 .752941176470588 .752941176470588], ... 'Callback','Myapply', ... 'ListboxTop',0, ... 'Position',[0.80859375 0.12 0.16 0.15], ... 'String','apply', ... 'Tag','Pushbutton1'); h1 = uicontrol('Parent',h0, ... 'Units','normalized', ... 'BackgroundColor',[1 1 1], ... 'Callback','Myedit', ... 'FontName','Times New Roman', ... 'FontSize',10, ... 'HorizontalAlignment','left', ... 'ListboxTop',0, ... 'Max',2, ... 'Position',[.55078125 .3273809523809524 .232421875 .4523809523809523], ... 'Style','edit', ... 'Tag','EditText1'); h1 = axes('Parent',h0, ... 'CameraUpVector',[0 1 0], ... 'CameraUpVectorMode','manual', ... 'Color',[1 1 1], ... 'ColorOrder',mat1, ... 'Position',[0.05 0.15 0.45 0.65], ... 'Tag','Axes1', ... 'XColor',[0 0 0], ... 'YColor',[0 0 0], ... 'ZColor',[0 0 0]); h2 = text('Parent',h1, ... 'Color',[0 0 0], ... 'HandleVisibility','off', ... 'HorizontalAlignment','center', ... 'Position',[0.4978165938864628 -0.2222222222222221 9.160254037844386], ... 'Tag','Axes1Text4', ... 'VerticalAlignment','cap'); set(get(h2,'Parent'),'XLabel',h2); h2 = text('Parent',h1, ... 'Color',[0 0 0], ... 'HandleVisibility','off', ... 'HorizontalAlignment','center', ... 'Position',[-0.1353711790393013 0.4907407407407408 9.160254037844386], ... 'Rotation',90, ... 'Tag','Axes1Text3', ... 'VerticalAlignment','baseline'); set(get(h2,'Parent'),'YLabel',h2); h2 = text('Parent',h1, ... 'Color',[0 0 0], ... 'HandleVisibility','off', ... 'HorizontalAlignment','right', ... 'Position',mat2, ... 'Tag','Axes1Text2', ... 'Visible','off'); set(get(h2,'Parent'),'ZLabel',h2); h2 = text('Parent',h1, ... 'Color',[0 0 0], ... 'HandleVisibility','off', ... 'HorizontalAlignment','center', ... 'Position',[0.4978165938864628 1.064814814814815 9.160254037844386], ... 'Tag','Axes1Text1', ... 'VerticalAlignment','bottom'); set(get(h2,'Parent'),'Title',h2); h1 = uicontrol('Parent',h0, ... 'Units','normalized', ... 'FontName','隶书', ... 'FontSize',13, ... 'HorizontalAlignment','left', ... 'ListboxTop',0, ... 'Position',[0.55 0.8 0.2 0.12], ... 'String','输入绘图指令', ... 'Style','text', ... 'Tag','StaticText1'); h1 = uicontrol('Parent',h0, ... 'Units','normalized', ... 'BackgroundColor',[.752941176470588 .752941176470588 .752941176470588], ... 'Callback','close(gcbf)', ... 'ListboxTop',0, ... 'Position',[0.5869921874999999 0.12 0.16 0.15], ... 'String','Close', ... 'Tag','Pushbutton2'); if nargout > 0, fig = h0; end
(责任编辑:泉水) |