2014-03-27 56 views

回答

2

您是否使用指南或「綱領性」鬼?以下是程序化gui的一個小例子;類似的概念可能適用於GUIDE。 (我個人比較喜歡的編程GUI路線的更大的靈活性,再加上我總是最終無可挽回地打破任何嚮導GUI的創建...)

反正幾件事情要在這個例子說明:

  1. 使用gui的圖形處理UserData字段來存儲「全局」信息。這是在回調之間傳遞數據的一種方式。
  2. 需要在「無限」循環中的暫停語句,以便處理來自cb_button2的中斷。從Matlab help開始:「如果回調執行的對象的Interruptible屬性爲開啓狀態,則回調可能會中斷,但只有當它或觸發的函數調用drawnow,figure,getframe,pause或waitfor 「。

    function my_gui(varargin) 
    
        mainfig = figure; 
    
        out.h_button1 = uicontrol(mainfig,... 
               'Style','pushbutton',... 
               'Units','Normalized',... 
               'Position',[0,0.5,1,0.5],... 
               'String','Button 1',... 
               'Callback',@cb_button1); 
    
        out.h_button2 = uicontrol(mainfig,... 
               'Style','pushbutton',... 
               'Units','Normalized',... 
               'Position',[0,0,1,0.5],... 
               'String','Button 2',... 
               'Callback',@cb_button2); 
    
        out.button2_flag = 0; %flag indicating whether button 2 has been pressed yet 
    
        set(mainfig,'UserData',out);%store "global" data in mainfig's UserData (for use by callbacks)   
    
    
    function cb_button1(varargin) 
    
        out = get(gcbf,'UserData'); %gcbf: handle of calling object's figure 
    
        while ~out.button2_flag 
         disp('Aaaahhh, infinite loop! Quick press Button 2!'); 
         out = get(gcbf,'UserData'); %reload "global" data 
         pause(0.1); %need this so this callback may be interrupted by cb_button2 
        end 
    
        disp('Thanks! I thought that would never end!'); 
    
    
    function cb_button2(varargin) 
        out = get(gcbf,'UserData'); %gcbf: handle of calling object's figure 
        out.button2_flag = 1; 
        set(gcbf,'UserData',out); %save changes to "global" data 
    
+0

+1的消息顯示:-)好了,並回答太 –

+0

而不是暫停(),你可以設置按鈕的回調是interuptable。它只是uicontrol的另一個屬性,所以您可以在創建時設置它。 –