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', ...
21
'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', ...
22
'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], ...
23
'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
(2)配套数据文件 [Myguizzy.m]
function Myguizzy
%假如Mygui1.m所在目录不是d:\\matbook5\\mdisk ,那么第<10>条就应做相应的改变。 %一定要保证本函数生成的 Mygui1.mat 与 Mygui1.m 在同一目录。 mat0=jet(64);
mat1=[ 0 0 1.0000 0 0.5000 0 1.0000 0 0 0 0.7500 0.7500 0.7500 0 0.7500 0.7500 0.7500 0 0.2500 0.2500 0.2500]; mat2=[-0.1179 1.3056 9.1603];
save d:\\matbook5\\mdisk\\Mygui1 %<10>
(3)如何利用本节所提供的文件产生图11.6.2-1所示的界面
? 把本节提供的Mygui1.m , Myguizzy.m , Mycolormap.m , Myapply.m , Myedit.m 五个文件
放在MATLAB的搜索路径上。
? 先运行Myguizzy.m ,创建数据文件Mygui1.mat 。 ? 运行Mygui1.m ,就可得到符合要求的界面。
24