matlab 建立 图形演示系统 书上找的,,但是发现不能运行

screen=get(0,'Screensize')W=screen(3);H=screen(4);figure('color',[1,1,1],'Position',[... 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);','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)')
展开
 我来答
百度网友db4b04636
推荐于2016-02-09 · TA获得超过2916个赞
知道小有建树答主
回答量:834
采纳率:92%
帮助的人:342万
展开全部

错误已修正,主要有以下几点:

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
展开全部

书中程序不能运行主要有两个问题:

  1. 双引号输入错误,应该是连着输入四个单引号。

  2. 代码:

    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未定义。

  3. 修改的一种做法(可以运行):将后面关于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)');

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式