Windowbuttonmotionfcn回调在句柄中看不到新字段

时间:2013-12-18 20:57:20

标签: matlab matlab-guide

我正在使用GUIDE在matlab中创建GUI。我想在鼠标左键关闭时在句柄中创建新字段,在鼠标移动时修改字段,在释放按钮时删除它。但是Windowbuttonmotionfcn回调没有看到那个新字段。所以,这是代码示例:

function fig_OpeningFcn(hObject, eventdata, handles, varargin)

   handles.output = hObject;
   guidata(hObject, handles);

   set(hObject,'windowbuttonmotionfcn',{@fh_wbmfcn,handles});
   set(hObject, 'windowbuttondownfcn',{@fh_wbdfcn, handles});
   set(hObject, 'windowbuttonupfcn',{@fh_wbufcn, handles});

function fh_wbmfcn(hObject, eventdata, handles)
   isfield(handles, 'new_field') % it is always 0, but should be 1 when button is down

function fh_wbdfcn(hObject, eventdata, handles)
   handles.new_field=1;
   guidata(hObject, handles);

function fh_wbufcn(hObject, eventdata, handles)
   if isfield(handles, 'new_field')
   handles=rmfield(handles,'new_field');
   guidata(hObject, handles);
   end

1 个答案:

答案 0 :(得分:0)

当您指定这样的回调时:

set(hObject,'windowbuttonmotionfcn',{@fh_wbmfcn,handles});

handles的值将始终是您在该点设置的值,因为您只传递了handles变量的值。

或者,以下内容应该有效:

set(hObject,'windowbuttonmotionfcn', @(obj,evt) fh_wbmfc(obj,evt, guidata(gcbo));

这将始终在回调的执行时评估guidata(gcbo) - 从而为您提供当前的handles值。

替代号码。 2(我个人的偏好):

% remove handles from callback definition
set(hObject,'windowbuttonmotionfcn', @fh_wbmfc);

% and get current handle value in the callback function:
def fh_wbmfc(object, evt)
    handles = guidata(object);
    ...