【问题标题】:Updating GUI handles when using callback使用回调时更新 GUI 句柄
【发布时间】:2014-01-03 22:41:48
【问题描述】:

我有一个由 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);

【问题讨论】:

  • 还有一件事是当我进入调试器模式并在handles.plot_holds 处停止它时,当它通过 hh 时,我看到 em 更新,但一旦 hhcall 返回完成,它就会回到以前的数字。

标签: matlab user-interface matlab-guide handles


【解决方案1】:

在任何句柄修改功能之后,您必须再次加载句柄:

% 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

【讨论】:

  • 太棒了!非常感谢你..我在想我必须复制并粘贴整个代码..我最终使用了你的第二个建议。第一个不太适合我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
相关资源
最近更新 更多