声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1644|回复: 1

[GUI界面] GUI编程遇到点问题,盼指教

[复制链接]
发表于 2011-3-19 15:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
程序运行结果如下:当拖动滑动条滑动时,可编辑文本框中的数值不实时改变。
程序如下:
clear all;
clc;
%
PlotSin=['Num=get(hs,''Value'');'...
             'subplot(ha);'...
             'x=0:0.1:Num*pi;'...
             'plot(x,sin(x));'...
             'axis([0 Num*pi -1 1]);'...
             'xlabel(''x'');'...
             'ylabel(''y=sin(x)'');'...
             'if get(hcGrid,''Value'')==1;'...
             'grid on;'...
             'else;'...
             'grid off;'...
             'end;'...
             ];
%         
PlotCos=['Num=get(hs,''Value'');'...
             'subplot(ha);'...
             'x=0:0.1:Num*pi;'...
             'plot(x,cos(x));'...
             'axis([0 Num*pi -1 1]);'...
             'xlabel(''x'');'...
             'ylabel(''y=cos(x)'');'...
             'if get(hcGrid,''Value'')==1;'...
             'grid on;'...
             'else;'...
             'grid off;'...
             'end;'...
             ];
%      
hf=figure('Position',[200 200 600 400],...
             'Name','Uicontrol1',...
             'NumberTitle','off');
ha=axes('Position',[0.4 0.1 0.5 0.7],...
              'Box','on');
%
hcGrid=uicontrol(hf,'Style','check',...
                         'Position',[50 240 100 20],...
                         'String','Grid on',...
                         'Value',1,...
                         'Callback',[...
                                                'if get(hcGrid,''Value'')==1;'...
                                                'Grid on;'...
                                                'else;'...
                                                'Grid off;'...
                                                'end;'...
                                                ]);
                                            
hbSin=uicontrol(hf,...
                       'Style','pushbutton',...
                       'Position',[50,140,100,30],...
                       'String','Plot sin(x)',...
                       'Callback',PlotSin);
hbCos=uicontrol(hf,...
                         'Style','Pushbutton',...
                         'Position',[50,100,100,30],...
                         'String','Plot cos(x)',...
                         'Callback',PlotCos);
hbClose=uicontrol(hf,...
                           'Position',[50 60 100 30],...
                           'String','Exit',...
                           'Callback','close(hf)');
                       
hrboxoff=uicontrol(hf,'Style','radio',...
                           'Position',[50 180 100 20],...
                           'String','Set box off',...
                           'Value',0,...
                           'Callback',[...
                           'set(hrboxon,''Value'',0);'...
                           'set(hrboxoff,''Value'',1);'...
                           'set(gca,''Box'',''off'');']);
hrboxon=uicontrol(hf,'Style','radio',...
                           'Position',[50 210 100 20],...
                           'String','Set box on',...
                           'Value',1,...
                           'Callback',[...
                           'set(hrboxon,''Value'',1);'...
                           'set(hrboxoff,''Value'',0);'...
                           'set(gca,''Box'',''on'');']);
% =========文本标签========
htDemo=uicontrol(hf,'Style','text',...
                           'Position',[300 350 100 20],...
                           'String','change x(N pi)');
heNum=uicontrol(hf,...
                         'Style','edit',...
                         'Position',[50 270 100 20],...
                         'String','4',...
                         'CallBack','set(heNum,''String'',num2str(Num))');                       
htpi=uicontrol(hf,'Style','text',...
                        'Position',[150 270 20 20],...
                        'String','Pi');                     
htminmax=uicontrol(hf,'Style','text',...
                              'Position',[50 310 100 20],...
                              'String','1pi          20pi');
hs=uicontrol(hf,'Style','slider',...
                   'Position',[50 310 100 20],...
                   'value',4,...
                   'Min',1,...
                   'Max',20,...
                   'callback',PlotSin);                          

回复
分享到:

使用道具 举报

发表于 2011-3-21 23:38 | 显示全部楼层
回复 1 # cl_1982614 的帖子

很久没玩GUI了, 个人也不习惯使用, 仅试修改下, 感觉应该还有地方可精进, LZ自行试试吧!
clear all; clc;
%
PlotSin=['Num=get(hs,''Value''); subplot(ha); x=0:0.1:Num*pi;'...
             'plot(x,sin(x)); axis([0 Num*pi -1 1]); xlabel(''x''); ylabel(''y=sin(x)'');'...
             'if get(hcGrid,''Value'')==1; grid on; else; grid off; end;'];
PlotCos=['Num=get(hs,''Value'');subplot(ha); x=0:0.1:Num*pi;'...
             'plot(x,cos(x)); axis([0 Num*pi -1 1]); xlabel(''x''); ylabel(''y=cos(x)'');'...
             'if get(hcGrid,''Value'')==1; grid on; else; grid off; end;'];
%      
hf=figure('Position',[200 200 600 400], 'Name','Uicontrol1', 'NumberTitle','off');
ha=axes('Position',[0.4 0.1 0.5 0.7], 'Box','on');
hcGrid=uicontrol(hf,'Style','check', 'Position',[50 240 100 20], 'String','Grid on', 'Value',1,...
                    'Callback','if get(hcGrid,''Value'')==1; Grid on; else; Grid off; end;');
hbSin=uicontrol(hf, 'Style','pushbutton', 'Position',[50,140,100,30], 'String','Plot sin(x)', 'Callback',PlotSin);
hbCos=uicontrol(hf, 'Style','Pushbutton', 'Position',[50,100,100,30], 'String','Plot cos(x)', 'Callback',PlotCos);
hbClose=uicontrol(hf, 'Position',[50 60 100 30], 'String','Exit', 'Callback','close(hf)');
hrboxoff=uicontrol(hf,'Style','radio', 'Position',[50 180 100 20], 'String','Set box off', 'Value',0, ...
                      'Callback','set(hrboxon,''Value'',0); set(hrboxoff,''Value'',1); set(gca,''Box'',''off'');');
hrboxon=uicontrol(hf,'Style','radio', 'Position',[50 210 100 20], 'String','Set box on', 'Value',1,...
                     'Callback', 'set(hrboxon,''Value'',1); set(hrboxoff,''Value'',0); set(gca,''Box'',''on'');');
htDemo=uicontrol(hf,'Style','text', 'Position',[300 350 100 20], 'String','change x(N pi)');
heNum=uicontrol(hf, 'Style','edit', 'Position',[50 270 100 20], 'String','4', ...
                    'CallBack','set(hs,''value'',str2num(get(heNum,''String''))); eval(PlotSin);');                       
htpi=uicontrol(hf,'Style','text','Position',[150 270 20 20], 'String','Pi');                     
htminmax=uicontrol(hf,'Style','text', 'Position',[50 330 100 20], 'String','1pi          20pi');
hs=uicontrol(hf,'Style','slider', 'Position',[50 310 100 20], 'value',4, 'Min',1, 'Max',20, ...
                'callback','set(heNum,''String'',num2str(get(hs,''Value''))); eval(PlotSin);');     

点评

多谢chaching老师的帮助  发表于 2011-3-22 09:41

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-29 09:10 , Processed in 0.102785 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表