使用 delete() MATLAB GUI 时出错

Error using delete() MATLAB GUI

当我按 'X' 关闭弹出窗口 window 时出现这样的错误。

这是我得到的错误:

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);']); 

您已将回调定义为字符向量,MATLAB evaluates in the base workspace where PopupWindow is not defined. You can instead use an 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));

给我们一个图形 window,当点击按钮时它会关闭:

请注意,我定义了匿名函数来接受和丢弃两个输入。这是因为图形对象回调accept 2 inputs by default,正在执行回调的对象句柄和事件数据结构。在这个简单的例子中,我们不需要任何一个,但有很多情况会保留此信息(例如,按钮按下回调的事件数据)。