matlab 建立 图形演示系统 书上找的,,但是发现不能运行
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')
hplot=uimenu(gcf,'Label','&Plot')
uimenu(hplot,'Label','since wave','callback',['t=-pi:pi/20:pi;','y=sin(t)','plot(t,y);','set(hgon,"Enable","on");','set(hgoff,"Enable","on");','set(hgon,"Enable","on");','set(hgon,"Enable","on");']);
%定义plot菜单栏
uimenu(hplot,'Label','cosine wave','callback',['t=-pi:pi/20:pi;','plot(t,cos(t));','set(hgon,"Enable","on");','set(hgoff,"Enable","on");','set(hgon,"Enable","on");','set(hgon,"Enable","on");']);
hoption=uimenu(gcf,'Label','&option')
hgon=uimenu(hoption,'Label','&Grid on','Callback','grid on','Enable','off');
hgoff=uimenu(hoption,'Label','&Grid off','Callback','grid off','Enable','on');
hgon=uimenu(hoption,'Label','&Box on','Callback','grid on','Enable','off');
hgoff=uimenu(hoption,'Label','&Box off','Callback','grid off','Enable','on');
%
hwincor=uimenu(hoption,'Label','&Window color','Separator','on');
%wrong
uimenu=(hwincor,'Label','&Blue','Accelerator','b','Callback','set(gcf,"color","b");');
uimenu=(hwincor,'Label','&Red','Accelerator','r','Callback','set(gcf,"color","r");');
uimenu=(hwincor,'Label','&Yellow','Accelerator','y','Callback','set(gcf,"color","y");');
uimenu=(hwincor,'Label','&White','Accelerator','w','Callback','set(gcf,"color","w");');
uimenu=(gcf,'Lable','&Quit','Callback','close(gcf)') 展开
错误已修正,主要有以下几点:
1)最后四局等号多余
2)单引号里面有单引号应该用两个单引号而不是双引号
3)其他一些逻辑错误,比如grid on和box on都叫hgon会有问题,把box on重命名为hbon
4)错误很多,不是抄错了,就是原书质量很差,不建议阅读。
下面是修改后的代码,效果还不错。
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')
hplot=uimenu(gcf,'Label','&Plot');
uimenu(hplot,'Label','since wave','callback',['t=-pi:pi/20:pi;','y=sin(t);','plot(t,y);box on;grid off;','set(hgon,''Enable'',''on'');','set(hgoff,''Enable'',''off'');','set(hbon,''Enable'',''off'');','set(hboff,''Enable'',''on'');']);
%定义plot菜单栏
uimenu(hplot,'Label','cosine wave','callback',['t=-pi:pi/20:pi;','plot(t,cos(t));;box on;grid off;','set(hgon,''Enable'',''on'');','set(hgoff,''Enable'',''off'');','set(hbon,''Enable'',''off'');','set(hboff,''Enable'',''on'');']);
hoption=uimenu(gcf,'Label','&option');
hgon=uimenu(hoption,'Label','&Grid on','Callback','grid on;set(hgoff,''Enable'',''on'');set(hgon,''Enable'',''off'');','Enable','off');
hgoff=uimenu(hoption,'Label','&Grid off','Callback','grid off;set(hgon,''Enable'',''on'');set(hgoff,''Enable'',''off'');','Enable','off');
hbon=uimenu(hoption,'Label','&Box on','Callback','box on;set(hboff,''Enable'',''on'');set(hbon,''Enable'',''off'');','Enable','off');
hboff=uimenu(hoption,'Label','&Box off','Callback','box off;set(hbon,''Enable'',''on'');set(hboff,''Enable'',''off'');','Enable','off');
%
hwincor=uimenu(hoption,'Label','&Window color','Separator','on');
%right
uimenu(hwincor,'Label','&Blue','Accelerator','b','Callback','set(gcf,''color'',''b'');');
uimenu(hwincor,'Label','&Red','Accelerator','r','Callback','set(gcf,''color'',''r'');');
uimenu(hwincor,'Label','&Yellow','Accelerator','y','Callback','set(gcf,''color'',''y'');');
uimenu(hwincor,'Label','&White','Accelerator','w','Callback','set(gcf,''color'',''w'');');
uimenu(gcf,'Label','&Quit','Callback','close(gcf)')
2018-08-24
书中程序不能运行主要有两个问题:
双引号输入错误,应该是连着输入四个单引号。
代码:
uimenu(hplot,'Label','since wave','callback',...
['t=-pi:pi/20:pi;','y=sin(t)','plot(t,y);','set(hgon,"Enable","on");','set(hgoff,"Enable","on");','set(hbon,"Enable","on");','set(hboff,"Enable","on");']);
中句柄变量:hgon、hgoff、hbon、hboff未定义。
修改的一种做法(可以运行):将后面关于hgon、hgoff、hbon、hboff定义的代码提前。
完整代码如下:
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');
% 定义Plot,Option一级菜单名
hplot = uimenu(gcf,'Label','&Plot');
hoption = uimenu(gcf,'Label','&Option');
% 定义Option菜单项(提前定义变量hgon、hgoff、hbon、hboff)
hgon = uimenu(hoption,'Label','&Grid On',...
'Call','grid on','Enable','off');
hgoff = uimenu(hoption,'Label','&Grid off',...
'Call','grid off','Enable','off');
hbon = uimenu(hoption,'Label','&Box On',...
'Separator','on','Call','box on','Enable','off');
hboff = uimenu(hoption,'Label','&Box off',...
'Call','box off','Enable','off');% 定义Window Color 菜单项
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'');');
uimenu(hwincor,'Label','&Yellow',...
'Call','set(gcf,''Color'',''y'');');
uimenu(hwincor,'Label','&White',...
'Call','set(gcf,''Color'',''w'');');
% 定义Plot菜单项
uimenu(hplot,'Label','SineWave','Call',...
['t = -pi:pi/20:pi;','plot(t,sin(t));',...
'set(hgon,''Enable'',''on'');',...
'set(hgoff,''Enable'',''on'');',...
'set(hbon,''Enable'',''on'');',...
'set(hboff,''Enable'',''on'');']);
uimenu(hplot,'Label','Cosine Wave','Call',...
['t = -pi:pi/20:pi;','plot(t,cos(t));',...
'set(hgon,''Enable'',''on'');',...
'set(hgoff,''Enable'',''on'');',...
'set(hbon,''Enable'',''on'');',...
'set(hboff,''Enable'',''on'');']);
% 定义Quit菜单项
uimenu(gcf,'Label','&Quit',...
'Call','close(gcf)');