2013-12-16 77 views
1

我有一個GUIDE製作的GUI,我無法弄清楚如何在回調中調用回調時更新GUI句柄。因此,例如在調用該函數的功能我只有以下幾點:使用回調更新GUI句柄

function start_ss_Callback(hObject, eventdata, handles) 
% hObject handle to start_ss (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 

% Hints: get(hObject,'String') returns contents of start_ss as text 
%  str2double(get(hObject,'String')) returns contents of start_ss as a double 
start_hh_Callback(hObject, eventdata, handles) 

start_hh_Callback我有如下的代碼,但我handles.plot_holds我有guidata(hObject, handles)不會不顧事實更新。這是因爲我將它用作函數嗎?如果我只是通過start_hh_Callback本身而不是通過start_ss_Callback,它會更新。但是,如果我在start_ss_Callback這樣的回調中使用它,它不會更新。

我希望我的問題很明確,讓我知道你是否需要澄清。

function start_hh_Callback(hObject, eventdata, handles) 
% hObject handle to start_hh (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 

% Hints: get(hObject,'String') returns contents of start_hh as text 
%  str2double(get(hObject,'String')) returns contents of start_hh as a double 

axes(handles.axes1) 
if isempty(handles.plot_holds) 
    cla 
    plot_button_Callback(hObject, eventdata, handles) 
else 
    % in the event the time is changed after more than 1 plot is held then 
    % the additional plot times will be re evaluated for each parameter and 
    % the new indicies will take into affect. 
    myParams = get(handles.load_params_button,'string'); 
    mystartDD = str2num(get(handles.start_dd,'string')); 
    mystartHH = str2num(get(handles.start_hh,'string')); 
    mystartMM = str2num(get(handles.start_mm,'string')); 
    mystartSS = str2num(get(handles.start_ss,'string')); 

    myendDD = str2num(get(handles.end_dd,'string')); 
    myendHH = str2num(get(handles.end_hh,'string')); 
    myendMM = str2num(get(handles.end_mm,'string')); 
    myendSS = str2num(get(handles.end_ss,'string')); 

    startFromStart = (mystartDD)*60*60*24 + (mystartHH-handles.startHour)*60*60 ... 
     + (mystartMM-handles.startMinute)*60 + (mystartSS-handles.startSecond); 
    endFromStart = (myendDD)*60*60*24 + (myendHH-handles.startHour)*60*60 ... 
     + (myendMM-handles.startMinute)*60 + (myendSS-handles.startSecond); 

    iStart = find(handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Time > (startFromStart+handles.startTimeOffset),1); 
    iEnd = find(handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Time > (endFromStart+handles.startTimeOffset),1); 
    if isempty(iEnd) 
     iEnd = length(handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Time); 
    end 

    cla 
    hold off 

    plot(handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Time(iStart:iEnd),... 
     handles.load.dataGST.(handles.myParams{handles.plot_holds(1,1)}).(handles.myPackets{handles.plot_holds(1,1)}{handles.plot_holds(1,2)}).Data(iStart:iEnd)) 

    grid minor 
    box on 

    handles.plot_holds(1,3) = iStart; 
    handles.plot_holds(1,4) = iEnd; 

    if size(handles.plot_holds,1)>1 
     hold all 
     for ii = 2:size(handles.plot_holds) 
      iStart = find(handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Time > (startFromStart+handles.startTimeOffset),1); 
      iEnd = find(handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Time > (endFromStart+handles.startTimeOffset),1); 
      if isempty(iEnd) 
       iEnd = length(handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Time); 
      end 
      plot(handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Time(iStart:iEnd),... 
       handles.load.dataGST.(handles.myParams{handles.plot_holds(ii,1)}).(handles.myPackets{handles.plot_holds(ii,1)}{handles.plot_holds(ii,2)}).Data(iStart:iEnd)) 
      handles.plot_holds(ii,3) = iStart; 
      handles.plot_holds(ii,4) = iEnd; 
     end 
    end 
end 

legend(handles.myLegends{1:length(handles.myLegends)}) 

guidata(hObject, handles); 
+0

一件事是,當我進入調試模式,並在handles.plot_holds我看到EM停止當它通過hh時更新,但一旦hhcall返回完成,它將返回到之前的數字。 – Niseonna

回答

3

你必須再次加載手柄,手柄任何修飾功能後:

% this modifies and writes the handles to the guidata 
start_hh_Callback(hObject, eventdata, handles); 
% now read back the updated value 
handles = guidata(hObject); 

或者你也可以讓handles返回值的回叫。 通常當它被用作回調函數時會被忽略,當它用作「正常」函數時,它可以避免從guidata重新讀取數據。 您的代碼可能會再看看這樣的:

handles = start_hh_Callback(hObject, eventdata, handles); 

與你的回調重新定義爲:

function [handles] = start_hh_Callback(hObject, eventdata, handles) 
... 
end 
+0

真棒!非常感謝你..我想我必須複製和粘貼整個代碼..我結束了使用你的第二個建議。第一個不適合我。 – Niseonna