exm14.m

来自「电子社08年的《精通MATLABG UI设计》一书的所有代码」· M 代码 · 共 87 行

M
87
字号
clear
clc
varX=['NumStr=get(heNum,''String'');',...             %调用过程
        'Num=str2num(NumStr);',...
       'x=0:0.1:Num*pi;' ];
hf=figure('Position',[200 200 600 400] ,...
           'Name','Uicontrol1' ,...
           'NumberTitle','off');
ha=axes('Position',[0.4 0.1 0.5 0.7],...
            'Box','on');
hbSin=uicontrol(hf,...
                'Style','pushbutton',...                    %sin按钮
                'Position',[50,140,100,30],...
                'String','Plot sin(x)',...
                'CallBack',...
                [varX,...
                'subplot(ha);'...
                  'plot(x,sin(x));'...                        %绘制sin
                  'axis([0 Num*pi -1 1]);'...
                  'xlabel(''x'');'...
                 'ylabel(''y=sin(x)'');'...
                  'if get(hcGrid,''Value'')==1;'... 
                   'grid on;'...
                 'else;'...
                   'grid off;'...
                'end;'...   
                ]);  
hbCos=uicontrol(hf,...
                'Style','pushbutton',...                     %cos按钮
                'Position',[50,100,100,30],...
                'String','Plot cos(x)',...
                'CallBack',...
                [varX,...
                'subplot(ha);'...
                'plot(x,cos(x));'...                          %绘制cos
                 'axis([0 Num*pi -1 1]);'...
                'xlabel(''x'');'...
                 'ylabel(''y=cos(x)'');'...
                 'if get(hcGrid,''Value'')==1;'...        %判断是否选中
                'grid on;'...
                  'else;'...
                'grid off;'...
                'end;'... 
                ]);             
hbClose=uicontrol(hf,...
                'Style','pushbutton',...
                'Position',[50,60,100,30],...
                'String','Exit',...
                'CallBack','close(hf)'); 
hrboxoff=uicontrol(hf,'Style','radio',...              %单选按钮off
               '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',...                   %单选按钮on
                'Value',1,...
                'CallBack',[...
                               'set(hrboxon,''Value'',1);'...
                              'set(hrboxoff,''Value'',0);'...
                              'set(gca,''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;'...
                              ]);  
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',...                              %默认输入为4
                   'CallBack',varX);   
htpi=uicontrol(hf,'Style','text',...
                  'Position',[150 270 20 20],...
                  'String','Pi'); 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?