screen=get(0,'screensize')%0是屏幕的句柄
w=screen(3);H=screen(4);
%figure('color',[0.9,1,1],'position',[0.5*H,0.2*H,0.5*w,0.3*H],'name','图形演示系统','numbertitle','off','menubar','none');
%figure('color',[0.9,1,1],'position',[0.5*H,0.2*H,0.5*w,0.3*H],'name','图形演示系统','numbertitle','on','menubar','none');
figure('color',[0.9,1,1],'position',[0.5*H,0.2*H,0.5*w,0.3*H],'name','图形演示系统','numbertitle','on');
%定义plot菜单项
hplot=uimenu(gcf,'label','&plot');%设定一个plot选项
uimenu(hplot,'label','sine wave','call',['t=-pi:pi/20:pi;','plot(t,sin(t));',...
'set(hgon,''enable'',''on'');','set(hgoff,''enable'',''on'');',... %enable 决定这个属性控制菜单项的可选择性
'set(hbon,''enable'',''on'');','set(hboff,''enable'',''on'');']) %call 是调用运行命令
uimenu(hplot,'label','cosine wave','call',['t=-pi:pi/20:pi;','plot(t,cos(t));',... % label 是显示项
'set(hgon,''enable'',''on'');','set(hgoff,''enable'',''on'');',...
'set(hbon,''enable'',''on'');','set(hboff,''enable'',''on'');'])
%定义option菜单项
hoption=uimenu(gcf,'label','&option');
hgon=uimenu(hoption,'label','&grid on','call','grid on','enable','on'); %uimenu包办了get和set的功能
hbon=uimenu(hoption,'label','&grid off','call','grid off','enable','on');
hgoff=uimenu(hoption,'label','&box on','separator','on','call','box on','enable','on');
hboff=uimenu(hoption,'label','&box off','call','box off','enable','on');
hwincor=uimenu(hoption,'label','&window color','separator','on');
uimenu(hwincor,'label','&red','accelerator','r','call','set(gcf,''color'',''r'');');
uimenu(hwincor,'label','&blue','accelerator','b','call','set(gcf,''color'',''b'');');%accelerator 用于设置快捷键
uimenu(hwincor,'label','&yellow','call','set(gcf,''color'',''y'');');
uimenu(hwincor,'label','&white','call','set(gcf,''color'',''b'');');
%不断以上一层的句柄为中心层次递进
%定义quit菜单项
uimenu(gcf,'label','&quit','call','close(gcf)');
%快捷菜单
%1 利用uicontextmenu函数建立快捷菜单
%2 利用uimenu 函数为快捷菜单建立菜单项
%3 利用set函数将该快捷菜单和某图形对象建立联系
%绘制曲线y=(2*e^(-0.5*x))*sin(2*pi*x),并建立一个与之联系的快捷菜单,用以控制曲线的线型和曲线宽度
x=0: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(hls,'label','虚线','call','set(hl,''linestyle'','':'');');
uimenu(hls,'label','实线','call','set(hl,''linestyle'',''-'');');
uimenu(hlw,'label','加宽','call','set(hl,''linewidth'',2);');
uimenu(hlw,'label','变细','call','set(hl,''linewidth'',0.5);');
set(hl,'uicontextmenu',hc)
%对话框设计
pbstart=uicontrol(gcf,'style','push','position',[40,5,100,25],'string','start plot',...%spush为默认按钮点了出现,位置,string为按钮显示的文字说明,callback为按下按钮的反应
'callback','t=0:pi/20:2*pi;plot(t,sin(t));axis([0,2*pi,-1,1])'); %callback 中用;来间隔
pbgrid=uicontrol(gcf,'style','toggle','position',[170,5,100,25],'string','grid','callback','grid');%toggle为双位按钮,点了出现,再点消失
%建立单选按钮,用来设置图形窗口的颜色,只能选择一种颜色
htxt=uicontrol(gcf,'style','text','string','color option','position',[200,130,150,20]);
%建立单选按钮
hr=uicontrol(gcf,'style','radio','string','red','position',[200,100,150,25],'value',1,... %radio为单选按钮,value 0表示圈圈里为空 value 1则有黑点,这一行的value定义了初始显示状态
'callback',['set(hr,''value'',1);','set(hb,''value'',0);','set(hy,''value'',0);','set(gcf,''color'',''r'')']);
hb=uicontrol(gcf,'style','radio','string','blue','position',[200,75,150,25],...
'callback',['set(hb,''value'',1);','set(hr,''value'',0);','set(hy,''value'',0);','set(gcf,''color'',''b'')']);
hy=uicontrol(gcf,'style','radio','string','yellow','position',[200,50,150,25],...
'callback',['set(hy,''value'',1);','set(hb,''value'',0);','set(hr,''value'',0);','set(gcf,''color'',''y'')']);
%建立复选框
%callback中可使用if语句
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(hp,''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'',''b'');',...
'if get(hc,''value'')==1,','set(gcf,''color'',''g''),','end']);
hn=uicontrol(gcf,'style','check','units','normalized','position',[.1,.2,.25,.1],'string','myname','callback',['set(gcf,''name'',''复选框未选中'');',...
'if get(hn,''value'')==1,','set(gcf,''name'',''复选框被选中''),','end']);
%建立下拉列表框
%callback中先定义a的众选项再get value
hpop=uicontrol(gcf,'style','popup','string','red|blue|green|yellow','position',[100,100,100,100],'callback',['a=[''r'',''b'',''g'',''y''];'...
'set(gcf,''color'',a(get(hpop,''value'')))']);%选择与否 在 a(get(hpop,''value'')) 中体现
%建立一个文本框,并加边框
ftdir=uicontrol(gcf,'style','frame','back','y','position',[30,180,120,100]);
edmulti=uicontrol(gcf,'style','edit','string','matlab is a language of technical computing.','position',[50,200,75,55],'max',2,'back','g');
%edmulti=uicontrol(gcf,'style','edit','string','matlab is a language of technical computing.','position',[50,200,75,55],'max',0.5,'back','w');
%建立两个滑动条,分别用于设置图形窗口的宽度和高度,并利用静态文本说明对象,标出滑动条的数值范围及当前值
fig=figure('position',[20,20,400,300])%整体图像的位置
hsli1=uicontrol(fig,'style','slider','position',[50,50,120,20],'min',200,'max',800,'value',800,'back','g',...% value 800为条形的初始值
'callback',['set(azmcur,''string'','...
'num2str(get(hsli1,''value'')));',...%slider的位置
'set(gcf,''position'',[20,20,get(hsli1,''value''),300]);']); %实时改变宽度
hsli2=uicontrol(fig,'style','slider','position',[240,50,120,20],'min',100,'max',600,'value',300,'back','b',... %!!! 这几个地方必须分段,不然电脑无法识别'是前句的还是后一句的,用空格隔开也行如下行
'callback',['set(elvcur,''string'',' 'num2str(get(hsli2,''value'')));',...
'set(gcf,''position'',[20,20,400,get(hsli2,''value'');])']);
%用静态文本标出最小值
azmmin=uicontrol(fig,'style','text','position',[20,50,30,20],'string',num2str(get(hsli1,'min')));
elvmin=uicontrol(fig,'style','text','position',[210,50,30,20],'string',num2str(get(hsli2,'min')));
%用静态文本标出最大值
azmmax=uicontrol(fig,'style','text','position',[170,50,30,20],'string',num2str(get(hsli1,'max')));
elvmax=uicontrol(fig,'style','text','position',[360,50,30,20],'string',num2str(get(hsli2,'max')));
%用静态文本标出当前设置的宽度和高度
azmlabel=uicontrol(fig,'style','text','position',[50,80,65,20],'string','width');
elvlabel=uicontrol(fig,'style','text','position',[240,80,65,20],'string','height');
azmcur=uicontrol(fig,'style','text','position',[120,80,50,20],'string','num2str(get(hsli1,''value'')');%或者用下一行的格式 本行是将数值转成字符串显示 下行是数值
elvcur=uicontrol(fig,'style','text','position',[310,80,50,20],'string',num2str(get(hsli2,'value')));
%建立“数值转换”对话框。在左边输入一个十进制整数和2~16之间的数,单击“转换”按钮能在右边得到十进制数对应的二~十六进制字符串,单击“退出”按钮退出对话框
hf=figure('color',[0,1,1],'position',[100,200,400,200],'name','数值转换','numbertitle','off','menubar','none');
uicontrol(hf,'style','text','units','normalized','position',[0.05,0.8,0.45,0.1],'horizontal','center','string','输入框','back',[0,1,1]);
uicontrol(hf,'style','text','units','normalized','position',[0.5,0.8,0.45,0.1],'horizontal','center','string','输出框','back',[0,1,1]);
uicontrol(hf,'style','frame','units','normalized','position',[0.04,0.33,0.45,0.45],'back',[0,1,1]);
uicontrol(hf,'style','text','units','normalized','position',[0.05,0.6,0.25,0.1],'horizontal','center','strin
评论0