1、Matlab 图形 用户界面设计,8.1菜单设计,建立用户菜单 调用格式: 一级菜单项句柄 uimenu(图形窗口句柄,属性名1,属性值1,) 子菜单项句柄 uimenu(一级菜单项句柄,属性名1,属性值1,),菜单对象常用属性 Label:该属性的取值是字符串,用于定义菜单项的名字。 Accelerator:该属性的取值是任意字母,用于定义菜单项的快捷键。 Callback:该属性的取值是字符串,可以是某个M文件或一组Matlab命令。,8.1菜单设计,菜单对象常用属性 Checked:该属性的取值是on或off,该属性为菜单项定义一个指示标记,可以用这个特性指明菜单项是否已选中。 Enab
2、le:该属性的取值是on或off,该属性控制菜单项的可选则性。 Position:该属性的取值是数值,用于定义一级菜单项在菜单条上的相对位置或子菜单在菜单组内的相对位置。 Separator:该属性的取值是on或off ,用于定义菜单项是否加分隔线。,8.1菜单设计,例8.1 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)
3、;,8.1菜单设计,%定义plot菜单项 hplot = uimenu(gcf, Label, ,8.1菜单设计,%定义option菜单项 hoption = uimenu(gcf, Label, ,8.1菜单设计,%定义quit菜单项 uimenu(gcf,Label,8.1菜单设计,快捷菜单 x=pi: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
4、(hls,Label,虚线,Callback,set(hl,LineStyle,:);); uimenu(hls,Label,实线,Callback,set(hl,LineStyle,-);); uimenu(hlw,Label,加宽,Callback,set(hl,LineWidth,2);); uimenu(hlw,Label,变细,Callback,set(hl,LineWidth,0.5);); set(hl,UIcontextMenu,hc); %将该快捷菜单和曲线对象联系起来,8.1菜单设计,8.1菜单设计,8.2对话框设计,8.2.1对话框的控件 按钮(Push Button)
5、双位按钮(Toggle Button) 单选按钮(Radio Button) 复选框(Check Box) 列表框(List Box) 弹出框(Popup Menu) 编辑框(Edit Box) 滑动条(Slider) 静态文本(Static Text) 边框(Frame),8.2对话框设计,8.2对话框设计,h0=figure(toolbar,none,.position,200 150 450 250,.name,按钮的使用); x=0:0.5:2*pi; y=sin(x); h=plot(x,y); grid on,8.2对话框设计,test=.if i=1,.i=0;,.y=cos(x
6、);,.delete(h),.set(hm,string,正弦函数),.h=plot(x,y);,.grid on,.else if i=0,.i=1;,.y=sin(x);,.set(hm,string,余弦函数),.delete(h),.h=plot(x,y);,.grid on,.end,.end;,8.2对话框设计,hm=uicontrol(gcf,style,pushbutton,.string,余弦函数,.callback,test); i=1; set(hm,position,250 20 60 20); set(gca,position,0.2 0.2 0.6 0.6) tit
7、le(按钮的使用) hold on,建立复选框,用于设置图像窗口的某些属性,如大小,颜色,标题等。htxt = uicontrol(gcf,Style,text,Units,normalized,. Position,.1,.5,.25,.1,String,Set Windows Properties); hp = uicontrol(gcf,Style,check,Units,normalized,. Position,.1,.4,.25,.1,String,MyPosition,. CallBack,set(gcf,“Position“,10,10,300,250);,. if get(h
8、p,“Value“)=1,. set(gcf,“Position“,10,10,600,500),end); hc = uicontrol(gcf,Style,check,Units,normalized,. Position,.1,.3,.25,.1,String,MyColor,. CallBack,set(gcf,“color“,“w“);,. if get(hc,“Value“)=1,. set(gcf,“color“,“w“),end); hn = uicontrol(gcf,Style,check,Units,normalized,. Position,.1,.2,.25,.1,S
9、tring,MyName,. CallBack,set(gcf,“Name“,“复选框未选中“);,. if get(hn,“Value“)=1,. set(gcf,“Name“,“复选框被选中“),end);,8.2对话框设计,图形用户界面设计窗口 方法 FileNewGUI,8.3可视化图形用户界面设计,8.3可视化图形用户界面设计,8.3可视化图形用户界面设计,8.3可视化图形用户界面设计,8.3可视化图形用户界面设计,Select,Toggle Button,Push Button,Radio Button,Edit Button,Slider,ListBox,Axis,Check B
10、ox,Toggle Button,Static text,Popup Menu,Align Object,Menu Editor,Property Inspector,Object Browser,M-file Editor,例8.5,8.3可视化图形用户界面设计,8.3可视化图形用户界面设计,8.3可视化图形用户界面设计,8.3可视化图形用户界面设计,8.3可视化图形用户界面设计,8.3可视化图形用户界面设计,function eight_5_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no out
11、put args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to eight_5 (see VARARGIN),8.3可视化图形用户界面设计,handles.peaks = peaks(35); handles.membrane = mem
12、brane(5);x,y=meshgrid(-8:0.5:8); r=sqrt(x.2+y.2); sinc =sin(r)./(r+eps); handles.sinc=sinc; handles.current_data = handles.peaks; mesh(handles.current_data);,function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a
13、future version of MATLAB % handles structure with handles and user data (see GUIDATA)val = get(hObject,Value); str = get(hObject,String); switch strvalcase peakhandles.current_data = handles.peaks;case membranehandles.current_data = handles.membrane;case sinchandles.current_data = handles.sinc; endg
14、uidata(hObject,handles),function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) mesh(handles.current_data);,function varargout
15、 = test_12_5(varargin) global a b n,function test_12_5_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (se
16、e GUIDATA) % varargin command line arguments to test_12_5 (see VARARGIN)global a b n a=1;b=1;n=1; theta = -2*pi:0.5:2*pi; rho=a*cos(b+n*theta); polar(theta,rho,k);,function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global a b n a = str2double(get(hObject,String); theta = -2*pi:0.5:2*pi; rho=a*cos(b+n*theta); polar(theta,rho,b);,