我的matlab 的第一个gui工程
注意,在设置position属性之前一定要先设置 units,不然会出错,无法显示radiobutton,同时,注意callback函数的特质与fcn的一些特殊规定,比如使用maxvalue,总体而言,matlab功能强大,确实值得一试,也算打了一天这个东西。
注意,如果不设置units,或者设置units为normalized,那么matlab的长1默认为150 像素,宽 60
function mygui s.hf = figure; set(s.hf,'toolbar','figure','menubar','none','numbertitle','off','name','example window','units','normalized','position',[0.3 0.4 0.6 0.5]); s.haxes = axes('parent',s.hf,'position',[0.1 0.1 0.5 0.8]); x=0:pi/50:2*pi; y = sin(x); axes(s.haxes); s.plot = plot(x,y,'b','linewidth',1.5); set(gca,'fontangle','italic','xcolor',[1 0 0]); grid on title('我的坐标轴'); xlabel('x_轴'); ylabel('y_轴'); s.hgroup = uibuttongroup('parent',s.hf,'title','栅格控制','units','normalized','position',[0.7 0.7 0.1 0.2]); s.hRadio1 = uicontrol('parent',s.hgroup,'style','radiobutton','units','normalized','string','grid on','tag','radiobutton1','position',[0.1 0.7 0.8 0.2]); s.hRadio2 = uicontrol('parent',s.hgroup,'style','radiobutton','string','grid off','units','normalized','tag','radiobutton2','position',[0.1 0.1 0.8 0.2]); s.text1 = uicontrol('parent',s.hf,'style','text','string','曲线颜色','units','normalized','position',[0.7 0.6 0.1 0.05]); s.list = uicontrol('parent',s.hf,'style','listbox',... 'string',{'r','g','b'},... 'units','normalized',... 'position',[0.805 0.55 0.1 0.1]); s.text2 = uicontrol('parent',s.hf,'style','text','string','曲线线型','units','normalized','position',[0.7 0.45 0.1 0.05]); s.pop = uicontrol('parent',s.hf,'style','popupmenu','string',{'-';'- -';':';'- .';'none'},'units','normalized','position',[0.805 0.4 0.1 0.1]); set(s.hgroup,'SelectionChangeFcn',... {@buttongroup_selection,s}); set(s.list,'callback',{@list_callback,s}); set(s.pop,'callback',{@pop_callback,s}); s.menu = uimenu('Label','曲线颜色'); menu1=uimenu(s.menu,'Label','红色','Foregroundcolor',[1,0,0],'Callback',{@menu_callback,s}); menu2=uimenu(s.menu,'Label','绿色','Foregroundcolor',[0,1,0],'Callback',{@menu_callback,s}); menu3=uimenu(s.menu,'Label','蓝色','Foregroundcolor',[0,0,1],'Callback',{@menu_callback,s}); s.toolbar = uitoolbar(s.hf); a = rand(16,16,3); s.toggletool = uitoggletool(s.toolbar,'CData',a,'TooltipString','栅格控制'); set(s.toggletool,'oncallback',{@grid_on_callback,s}); set(s.toggletool,'offcallback',{@grid_off_callback,s}); function grid_on_callback(hobject,event,s) set(s.haxes,'xgrid','on','ygrid','on'); function grid_off_callback(hobject,event,s) set(s.haxes,'xgrid','off','ygrid','off'); function menu_callback(hobject,eventdata,s) value = get(hobject,'position'); switch value case 1 set(s.plot,'color','r'); case 2 set(s.plot,'color','g'); case 3 set(s.plot,'color','b'); end function buttongroup_selection(hobject,eventdata,s) tag = get(eventdata.NewValue,'tag'); switch tag case 'radiobutton1' set(s.haxes,'xgrid','on','ygrid','on'); case 'radiobutton2' set(s.haxes,'xgrid','off','ygrid','off'); end function list_callback(object,event,s) value = get(object,'value'); switch value case 1 set(s.plot,'color','r'); case 2 set(s.plot,'color','g'); case 3 set(s.plot,'color','b'); end function pop_callback(hobject,~,s) value = get(hobject,'value'); switch value case 1 set(s.plot,'linestyle','-','marker','none'); case 2 set(s.plot,'linestyle','- -','marker','none'); case 3 set(s.plot,'linestyle',':','marker','none'); case 4 set(s.plot,'linestyle','- .','marker','none'); case 5 set(s.plot,'linestyle','none','marker','none'); end