2013-12-16 4 views
1

У меня есть GUI, сделанный из GUIDE, и я не могу понять, как обновить дескриптор 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 вижу я их обновляется, когда он проходит через 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

Смежные вопросы