我正在设计一个Matlab GUI。在此 GUI 中,windowButtonMotion 不断接受输入。但是,我需要停止它一段时间,并向用户询问来自另一个图形的视觉输入。这个数字在输入后关闭,程序应该以同样的方式工作。
当弹出新图时,我用这段代码禁用了 WBM:
set(fighandle, 'WindowButtonMotionFcn', '');
问题是,输入后如何重新启动 WBM。
我不使用如下函数调用来调用 WBM:
set(fighandle, 'WindowButtonMotionFcn', @fun);
相反,我在 GUI 中的windowButtonMotion_Fcn
回调下编写代码。
最简单的方法是在更改回调之前存储回调,然后可以将其重置为相同的内容。无论您的回调是什么,这都将有效。
% Store the original
originalCallback = get(fighandle, 'WindowButtonMotionFcn');
% Disable the callback
set(fighandle, 'WindowButtonMotionFcn', '')
% Do stuff
% Now reset it
set(fighandle, 'WindowButtonMotionFcn', originalCallback)
如果这些情况未在同一函数中发生,则可以将之前的回调存储在图形的应用数据中。
% Get original callback and store within the appdata of the figure
setappdata(fighandle, 'motioncallback', get(fighandle, 'WindowButtonMotionFcn'));
% Disable the callback
set(fighandle, 'WindowButtonMotionFcn', '')
% Then from another function re-enable it
set(fighandle, 'WindowButtonMotionFcn', getappdata(fighandle, 'motioncallback'))
您的另一种选择是实际打开第二个图形作为模态图形,然后鼠标与背景图形的交互将被忽略。那么你就不必篡改WindowButtonMotionFcn
了。
fig2 = figure('WindowStyle', 'modal');
另一个答案很好,但我找到了另一种效果很好并且适合我的情况更简单的方法:
基本上,我只是使用以下命令将我不想与之交互的数字设置为暂时不可见:
set(fig_handle, 'visible', 'off')
在我的情况下,在设置图形时完成所有设置和计算需要几秒钟,如果有人碰巧将鼠标移到图形上,则 windowbuttonmotionfcn 将给出错误,所以我只是将图形设置为不可见,直到所有设置完成, 然后这样做:
set(fig_handle, 'visible', 'on')
这可能是也可能不是您希望在你的情况下发生的事情。