Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How can I ensure that GUI handles are updated when a callback is executed while a while loop in another callback is running?

조회 수: 1 (최근 30일)
I've got a while loop constantly running, with pause(0.08) at the top of the while loop to make it breakable. A pushbutton might be pressed at any time, triggering a callback function that changes the value of a key state variable (stored as handles.state). This state variable is examined in several if statements within the while loop, which call various helper functions. handles = guidata(hObject) is called after each of these function calls.
Right now, although the callback is being called and the value of the state variable is being changed within it, the state almost always returns to its previous value (1, previously assigned within the while loop) as soon as the debugger re-enters the while loop. Sometimes it enters the "if state == 2" case within the while loop, indicating the value assigned within the callback has been conserved, but this is sporadic and very occasional.
I can't share the full callback code due to confidentiality issues. However, the second-last line in the callback changes the variable value. The last line in the callback calls guidata(hObject, handles).
If I call handles = guidata(hObject) at the top of the while loop, it causes problems (a plot updating realtime stays at (0,0) perpetually).
How do I make sure handles.state keeps the value assigned to it in the callback, even after the debugger re-enters the while loop?
Here's my code for the while loop:
while Start_active
pause(0.08);
try
RxText = fscanf(handles.serConn);
set(handles.rawInput,'String',RxText);
handles.serConn.ValuesReceived;
flushinput(handles.serConn);
if length(RxText) > 10
processData(hObject, RxText, handles);
handles = guidata(hObject);
% 1st iteration
if handles.state == 0
getZeroValues(hObject, handles);
handles = guidata(hObject);
% to display calibration menu
calibrate(hObject, RxText, handles);
handles = guidata(hObject);
handles.state = 1;
guidata(handles, hObject);
% to scale data (after "Calibrate" clicked)
elseif handles.state == 2
scaleData(hObject, handles);
handles = guidata(hObject);
end
%handles = guidata(hObject);
h.XData(:) = handles.x;
h.YData(:) = handles.y;
drawnow();
end
catch e
disp(e)
end
end
  댓글 수: 1
Rik
Rik 2018년 8월 17일
Actually, it is the drawnow command that triggers callbacks to be executed. The handles=guidata(hObject); command should be re-loading your struct on every iteration (but only if length(RxText)>10).

답변 (0개)

제품


릴리스

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by