传递变量:
我建议使用 setappdata 和 getappdata 在 GUI 的回调之间传递变量。
您也可以阅读Share Data Among Callbacks 了解有关这方面的更多信息。
分配和禁用回调:
要禁用回调函数,您不需要将回调重新分配给什么都不做的函数,您可以简单地分配一个空数组:
% assign the 'KeyDownListener' function and pass one parameter ('var1') with it
set( h.fig, 'KeyPressFcn',{@KeyDownListener,var1})
% then later when you don't need it anymore:
% Disable the 'KeyPressFcn listener, assign 'empty' to it
set( h.fig, 'KeyPressFcn',[])
完整示例:
下面是一个最小的 GUI,它演示了如何捕获单个按键(和时间),然后将收集到的数据保存到应用程序的 appdata 空间中,并通过“显示”功能再次收集它们(您还可以对收集的数据做任何您想做的事情)。
SingleKeyPressDemo.m 的代码:
function h = SingleKeyPressDemo
% create a minimal figure with a button and 2 text fields
h.fig = figure ;
h.btn = uicontrol('Style','Pushbutton','String','Start capture',...
'units','Normalized','Position',[0.1 0.6 0.8 0.3],...
'Callback',@StartKeyCapture) ;
h.txtKey = uicontrol('Style','text','String','Key pressed:',...
'units','Normalized','Position',[0.1 0.4 0.8 0.1]) ;
h.txtTime = uicontrol('Style','text','String','Elapsed time:',...
'units','Normalized','Position',[0.1 0.3 0.8 0.1]) ;
% assign a callback to the KeyRelease event to intercept passing the
% eventdata to the command window
set(h.fig,'KeyReleaseFcn',@KeyReleased)
% initialise appdata variables to hold the captured key and the time
setappdata( h.fig , 'CapturedKey' , [] )
setappdata( h.fig , 'Elapsed_time' , [] )
% save the handle structure
guidata( h.fig , h)
function StartKeyCapture(hobj,~)
h = guidata( hobj ) ; % retrieve the handle structure
StartTime = tic ; % Start a stopwatch
% assigne the callback funtion, passing the stopwatch in parameter
set(h.fig,'KeyPressFcn',{@KeyDownListener,StartTime})
% disable the button until a key is pressed (also makes it loose the
% focus, which is handy otherwise the button keeps the focus and hides
% the 'KeyPressedFcn'
set( h.btn , 'Enable','off')
function KeyDownListener(hobj, key_obj, starting_time)
% Detect key pressed and time elapsed
Elapsed_time = toc(starting_time) ;
key_pressed = key_obj.Key;
h = guidata( hobj ) ; % retrieve the handle structure
setappdata( h.fig , 'CapturedKey' , key_pressed ) ; % save the captured key
setappdata( h.fig , 'Elapsed_time' , Elapsed_time ) ; % save the elapsed time
set(h.fig,'KeyPressFcn',[]) % remove listener so new key press will not trigger execution
set( h.btn , 'Enable','on') % re-enable the button for a new experiment
% (optional) do something after a key was pressed
display_results(h.fig) ;
function display_results(hobj)
h = guidata( hobj ) ; % retrieve the handle structure
% retrieve the saved data
CapturedKey = getappdata( h.fig , 'CapturedKey' ) ;
Elapsed_time = getappdata( h.fig , 'Elapsed_time' ) ;
% update display
set( h.txtKey , 'String', sprintf('Key pressed: %s',CapturedKey) ) ;
set( h.txtTime , 'String', sprintf('Elapsed time: %f ms',Elapsed_time) ) ;
function KeyReleased(~,~)
% do nothing
% The only purpose of this function is to intercept the KeyReleased event
% so it won't be automatically passed on to the command window.
在 Matlab 中以编程方式创建 GUI 非常冗长,重点关注 StartKeyCapture 和 KeyDownListener 两个函数中的代码来实现您的要求。
此示例将生成以下 GUI:
补充说明:
我还建议不要在 GUI 应用程序中使用 gcf。当调试或在控制台中打开图形时,这是一个方便的快捷方式,但在 GUI 中,对其自身元素的调用应尽可能独立。 MATLAB 提供了保存所有 GUI 元素(所有 uicontrol,包括主图)句柄的方法,因此您可以在需要时显式地调用它们。这降低了出错的风险(假设您的用户也在同一个 MATLAB 会话中运行其他图形,如果您的回调触发并在用户摆弄另一个图形时调用 gcf,您将尝试在不同的图形上执行代码超出预期的数字......这很容易导致错误)。
阅读guidata 上的文档以获取更多信息(和/或观察我在上面的示例中是如何使用它的)。
编辑:
为避免每次按下时焦点都回到命令窗口,您还必须为相应的KeyRelease事件定义一个回调函数,否则该事件将自动转发到命令窗口(这将占用焦点)。
一个干净的方法是简单地添加回调分配
set(h.fig,'KeyReleaseFcn',@KeyReleased)
在图形定义中一劳永逸(您不必在每个实验中设置/撤消此回调),然后定义一个空函数function KeyReleased(~,~),它什么都不做。这种方式在上面修改后的代码示例中实现。
另一种不使用额外空函数的方法是在赋值时简单地定义回调:
set(h.fig,'KeyReleaseFcn','disp([])')
这样您就不需要有一个空的KeyReleased 函数。
但是请注意,回调函数必须被定义(内联或在代码后面)。简单地将empty 分配给回调是行不通的。 (例如,以下两个选项都将无法拦截事件并将其转发到命令窗口:
set(h.fig,'KeyReleaseFcn','') % not working
set(h.fig,'KeyReleaseFcn',[]) % not working either