2017-09-19 55 views
1

當我按下'X'關閉彈出窗口時,出現這樣的錯誤。錯誤使用刪除()MATLAB GUI

這裏是我的錯誤:

Undefined function or variable 'PopupWindow'. 

Error while evaluating UIControl Callback 

這裏是我使用的代碼:

function PopupWindow = alertBox(figg,position,showtext,titlebar); 

    PopupWindow = uipanel('Parent',figg,'Units','pixels','Position',position,... 
      'BackGroundColor',CYAN,'BorderType','beveledout','ButtonDownFcn','','Visible','on'); 

    uicontrol('Parent',PopupWindow,'Units','pixels','Style','PushButton','String','X',... 
        'Position',[position(3)-margin+1 position(4)-margin+1 margin-2 margin-2],'Callback',... 
        ['delete(PopupWindow);']); 

回答

2

您已經定義了你的回調爲特徵向量,這MATLAB evaluates in the base workspace其中沒有定義PopupWindow。您可以改用anonymous function作爲回撥。

例如:

fig = figure(); 
a = uicontrol('Parent', fig, 'Style', 'Pushbutton', 'Units', 'Normalized', ... 
       'Position', [0.1 0.1 0.8 0.8], 'String', 'Delete Figure', ... 
       'Callback', @(h,e)delete(fig)); 

給了我們一個數字窗口將關閉按鈕被點擊時:我已經定義了匿名函數接受&拋

yay

注去掉兩個輸入。這是因爲圖形對象回調accept 2 inputs by default,其回調正在執行的對象的句柄和事件數據結構。在這種簡單的情況下,我們不需要任何一個,但是在許多情況下這些信息將被保留(例如,按鈕回叫的事件數據)。

+0

這非常明確和有用。非常感謝你! –