2013-12-18 40 views
0

我正在使用GUIDE在matlab中創建GUI。我想在鼠標左鍵關閉時在句柄中創建新字段,當鼠標移動時修改字段並在釋放按鈕時將其刪除。但Windowbuttonmotionfcn回調沒有看到這個新字段。所以,這裏是代碼示例:因爲你只有通過Windowbuttonmotionfcn回調沒有看到句柄中的新字段

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

handles值將永遠是你設置在這一點上一個,:

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 

回答

0

當您指定這樣的回調變量的值爲handles

或者,下面應該工作:

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

這將在回調的執行時間總是求guidata(gcbo) - 因此給你當前handles值。

替代no。 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); 
    ... 
相關問題