2015-07-12 14 views
0

更改布爾的狀態我創建了一個小例子程序,以證明我的問題(變量「option1selcted」不改變其值):如何使用回調從單選按鈕在MATLAB

classdef radioexample < handle 
    %radioexample2 
    % example for radiobuttuns 
     properties(Hidden) 
      % all elements of the GUI are properties of the class trechner 
      formMain; % "The MainWindow" 
      menuFile; % "The Menu Header" 
      % buttons 
      buttonTest 
      % radio items 
      radiogroup 
      radio1 
      radio2 
      % statis variable 
      option1selcted = true; 
     end 
     methods(Hidden) 
      function obj = radioexample 
       % Constructor Form Main 
       obj.formMain = figure('position',[400,400,600,260],'Visible','off'); 
       set(obj.formMain,'Name','Rradioexample','NumberTitle','off',... 
        'MenuBar','none','Resize','Off'); 
       % a menu for exit the program    
       obj.menuFile.main = uimenu('Label','File');    
       obj.menuFile.exit = uimenu(obj.menuFile.main,... 
        'Label','Exit','Callback',{@obj.close_Callback,obj}); 
       % radiobutton to select the mode 
       obj.radiogroup = uibuttongroup(obj.formMain,... 
        'Visible','on',... 
        'Units','pixels',... 
        'BackGroundColor',[0.8 0.8 0.8],... 
        'Position',[220 80 100 100]); 
        %'SelectionChangedFcn',@obj.bselection); 
       uicontrol(obj.radiogroup,... 
        'Style',... 
        'radiobutton',... 
        'BackGroundColor',[0.8 0.8 0.8],... 
        'String','Option 1',... 
        'Position',[10 70 70 20],... 
        'Callback',@obj.opt1_Callback,... 
        'HandleVisibility','off'); 
       uicontrol(obj.radiogroup,... 
        'Style',... 
        'radiobutton',... 
        'BackGroundColor',[0.8 0.8 0.8],... 
        'String','Option 2',... 
        'Position',[10 50 70 20],... 
        'Callback',@obj.opt2_Callback,... 
        'HandleVisibility','off');  
       obj.buttonTest = uicontrol('Style','pushbutton','String','Print radiostaus intop the Command Window',... 
        'Position',[40,200,260,60],... 
        'Callback',{@obj.test_Callback,obj,'test'},'Enable','On'); 
       set(obj.formMain,'Visible','on'); 
       fprintf('programm started'); 
      end 
     end 
     methods(Static,Access=private)    
      function close_Callback(~,~,obj) 
       % close window 
       close(obj.f) 
      end 
      function opt1_Callback(hObject, evt) 
       fprintf('switched to radio1 mode\n'); 
       obj.option1selcted = true; %<- does not change the variable? 
      end 
      function opt2_Callback(hObject, evt) 
       fprintf('switched to radio2 mode\n'); 
       %msgbox('switched to radio2 mode','Success'); 
       obj.option1selcted = false; %<- does not change the variable? 
      end 
      function test_Callback(~,~,obj,val) 
       fprintf('the status of the radiobutton is: %d\n', obj.option1selcted); 
      end  
     end 
     methods(Access=public,Hidden) 
      function disp(obj) 
      end 
     end 
    end 

要測試變量「option1selcted」的狀態testbutton將其狀態寫入matlab命令窗口。

回答

1

您不能在靜態方法內更改obj的值。定義非靜態方法塊內的回調函數。您需要爲輸入參數添加另一個~。我也在該按鈕的回調中解決了這個問題。

這裏是全碼:

classdef radioexample < handle 
    %radioexample2 
    % example for radiobuttuns 
     properties(Hidden) 
      % all elements of the GUI are properties of the class trechner 
      formMain; % "The MainWindow" 
      menuFile; % "The Menu Header" 
      % buttons 
      buttonTest 
      % radio items 
      radiogroup 
      radio1 
      radio2 
      % statis variable 
      option1selcted = true; 
     end 
     methods(Hidden) 
      function obj = radioexample 
       % Constructor Form Main 
       obj.formMain = figure('position',[400,400,600,260],'Visible','off'); 
       set(obj.formMain,'Name','Rradioexample','NumberTitle','off',... 
        'MenuBar','none','Resize','Off'); 
       % a menu for exit the program    
       obj.menuFile.main = uimenu('Label','File');    
       obj.menuFile.exit = uimenu(obj.menuFile.main,... 
        'Label','Exit','Callback',{@obj.close_Callback,obj}); 
       % radiobutton to select the mode 
       obj.radiogroup = uibuttongroup(obj.formMain,... 
        'Visible','on',... 
        'Units','pixels',... 
        'BackGroundColor',[0.8 0.8 0.8],... 
        'Position',[220 80 100 100]); 
        %'SelectionChangedFcn',@obj.bselection); 
       uicontrol(obj.radiogroup,... 
        'Style',... 
        'radiobutton',... 
        'BackGroundColor',[0.8 0.8 0.8],... 
        'String','Option 1',... 
        'Position',[10 70 70 20],... 
        'Callback',@obj.opt1_Callback,... 
        'HandleVisibility','off'); 
       uicontrol(obj.radiogroup,... 
        'Style',... 
        'radiobutton',... 
        'BackGroundColor',[0.8 0.8 0.8],... 
        'String','Option 2',... 
        'Position',[10 50 70 20],... 
        'Callback',@obj.opt2_Callback,... 
        'HandleVisibility','off');  
       obj.buttonTest = uicontrol('Style','pushbutton','String','Print radiostaus intop the Command Window',... 
        'Position',[40,200,260,60],... 
        'Callback',{@obj.test_Callback,obj,'test'},'Enable','On'); 
       set(obj.formMain,'Visible','on'); 
       fprintf('programm started'); 
      end 

      function opt1_Callback(~,hObject,evt) 
       fprintf('switched to radio1 mode\n'); 
       obj.option1selcted = true; 
      end 
      function opt2_Callback(~,hObject,evt) 
       fprintf('switched to radio2 mode\n'); 
       %msgbox('switched to radio2 mode','Success'); 
       obj.option1selcted = false; 
      end 
      function test_Callback(~,~,~,obj,val) 
       fprintf('the status of the radiobutton is: %d\n', obj.option1selcted); 
      end 

     end 
     methods(Static,Access=private)    
      function close_Callback(~,~,obj) 
       % close window 
       close(obj.f) 
      end 

     end 
     methods(Access=public,Hidden) 
      function disp(obj) 
      end 
     end 
    end 
+0

爲了正確地工作,它必須被更改爲: '函數opt1_Callback(OBJ,hObject,EVT) fprintf中( '切換到收音機1模式\ n' ); obj.option1selcted = true; end' – kimliv