《科学计算科学计算 (47).pdf》由会员分享,可在线阅读,更多相关《科学计算科学计算 (47).pdf(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、8.3 Ways to Develop Apps8.3 Ways to Develop AppsUI ComponentsControl ObjectsMenu ObjectsGraphical User Interface(GUI)is a window for user-computer interaction.Ways to develop AppBuild an App programmatically using MATLAB functionsDevelop an App visually using GUIDE 1 1UI ComponentsUI Components(1)Co
2、mmon Componentsfor inputting and outputting dataSuch as edit,text,listbox,sliderto implement confirmation and optionSuch as pushbutton,togglebutton,radiobutton,checkbox,2 2C Control ontrol OObjects bjects(2)Create user interface controlMATLAB provides the uicontrol function to create user interface
3、control object.Syntaxhandle=uicontrol(parent,property1,value1,property2,value2,)Property and corresponding value specify the characteristics of uicontrol.If the parent is omitted,create control objects in the current figure.2 2C Control ontrol OObjects bjects(3)UIControl PropertiesStyle:Style of UIC
4、ontrol object.For example,pushbutton means button,edit meanseditable text field.,etc.String:Text to display.Tag:Objects identifierEnable:Operational state of user interface control.Position:Location and size,specified as a four-element vector of the form x,y,w,h.Callback:Primary callback function,sp
5、ecified as one of these values:Afunction handle.Acell array in which the first element is a function handle.Acharacter vector containing a valid MATLAB expression.2 2C Control ontrol OObjects bjects Example 1:Create three buttons in a figure window.When a button is clicked,plot sine curve,display or
6、 hide the grid of axes,clear the graph in the axes.ha=axes(Units,pixels,Position,40,40,360,360);ptgrid=uicontrol(Style,pushbutton,.String,Grid,Position,450,120,48,20,.Callback,grid);btncla=uicontrol(Style,pushbutton,.String,Clear,Position,450,80,48,20,.Callback,cla);btnplot=uicontrol(Style,pushbutto
7、n,.String,Plot,Position,450,160,48,20);function plot_sin(source,callbackdata)t=-pi:pi/20:pi;plot(t,sin(t);endAcallback function defines how an object responds to an event.Syntaxfunction FunctionName(source,eventdata)Argument source is the the source control that triggered the callback,and eventdata
8、provides eventdata to the callback function.For example,define the function for the Draw button in Example 1 as the callback functionnaming plot_sin.ha=axes(Units,pixels,Position,40,40,360,360);ptgrid=uicontrol(Style,pushbutton,.String,Grid,Position,450,120,48,20,.Callback,grid);btncla=uicontrol(Sty
9、le,pushbutton,.String,Clear,Position,450,80,48,20,.Callback,cla);btnplot=uicontrol(Style,pushbutton,.String,Plot,Position,450,160,48,20);%Set the Callback property of the plot button to plot_sinfunction handlebtnplot.Callback=plot_sin;(1)Create menu objectThe uimenu function is used to create menu o
10、r menu items.Syntaxhandle=uimenu(parent,property1,value1,property2,value2,.)When creating a first-level menu,the first parameter is the figure handle.When creating a submenu,the first parameter is the upper menu object.3 3MenuMenu OObject bject(2)Menu PropertiesLabel:The label that appears on the me
11、nu.Accelerator:a keyboard shortcut for selecting a menu item.Checked:Whether the menu item checked or unchecked.Enable:Makes the user can or cannot interact with it.Separator:Add a dividing line above the menu item.3 3MenuMenu OObject bject hopt=uimenu(gcf,Label,Figure options,Accelerator,L);hLStyle
12、=uimenu(hopt,Label,Linestyle,Tag,LStyle,Enable,off);hL_Solid=uimenu(hLStyle,Label,Solid line,.Tag,Solid,Callback,MLine_Type);hL_Dotted=uimenu(hLStyle,Label,Dotted line,.Tag,Dotted,Callback,MLine_Type);hL_Dashed=uimenu(hLStyle,Label,Double dash,.Tag,Dashed,Callback,MLine_Type);Example 2:Add Graphics
13、Options menu to figure of Example 1,which includes asubmenu item Line Style.The submenu has 3 menu items,which called Solid,Dotted and Dashed,respectively.function MLine_Type(source,callbackdata)hline=findobj(Type,line);if strcmp(source.Tag,Solid)=1hline.LineStyle=-;elseif strcmp(source.Tag,Dotted)=
14、1hline.LineStyle=:;elseif strcmp(source.Tag,Dashed)=1hline.LineStyle=-;endendCreate a new function file MLine_Type.m with following codes:function plot_sin(source,callbackdata)t=-pi:pi/20:pi;plot(t,sin(t);h1=findobj(Tag,LStyle);h1.Enable=On;%Make the linestyle menu item availableendAdd the following
15、 statements in the callback function naming plot_sin:First run the program in Example 1 to generate the user interface.Then run the program inExample 2.Add the Graphics Options menu item to the interface.At this time,the LineStyle menu is not available.Click the Draw button to draw a sine curve curve,and the Line Style menu becomesavailable.At this time,we can change the line style.For instance,change the line style toDashed.