Another handles and guidata(hObject) in a Callback question

1 次查看(过去 30 天)
I've been do a lot of reading about how to properly use the "handles" variable and how to interact with the master copy through guidata(hObject). My first question is when to use it. For instance, I have a callback function.
function resetButton_Callback(hObject, eventdata, handles)
% hObject handle to resetButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Do I need to call handles=guidata(hObject) right away or is it safe to assume that at the instant the callback occurs, the difference in time between what would be in "handles" through the function call would be identical to what would be through "guidata(hObject)"?
But what I haven't been able to understand properly is say I have a timer function for my GUI and updates the handles ever 0.1 second. How do I work with the handles then?
% GUI initialize
function Gui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.count = 0;
handles.timerData = timer('TimerFcn',{@timerFunction,hObject},'StartDelay',1,'ExecutionMode','fixedRate','BusyMode','drop','Period',0.1);
guidata(hObject,handles);
end
% The Timer that has a counting variable
function timerFunction(hObect,eventdata,theGUI)
handles = guidata(theGUI);
handles.count = handles.count + 1;
disp(handles.count);
guidata(theGUI,handles);
end
% Reset button
function resetButton_Callback(hObject, eventdata, handles)
handles.count = 0;
guidata(hObject,handles);
end
When I click the resetButton, the count never resets. However, if I do the following, I can see the count updating inside the callback. So I know it's getting the latest handles. It's just not taking the new handles back to the timer. The timer function is always overwriting the handles.
% Reset button
function resetButton_Callback(hObject, eventdata, handles)
disp(handles.count)
handles.count = 0;
guidata(hObject,handles);
end
If I remove the guidata(theGUI,handles) from the timer, the timer never updates. And if I have the resetButton set a value to count, the data is changed.
% The Timer that has a counting variable
function timerFunction(hObect,eventdata,theGUI)
handles = guidata(theGUI);
handles.count = handles.count + 1;
disp(handles.count);
% guidata(theGUI,handles);
end
% Reset button
function resetButton_Callback(hObject, eventdata, handles)
handles.count = 10;
guidata(hObject,handles);
end
Initially count = 1, after button is pressed count = 11. It proves the handles are being overwritten by the timer.
Is the answer to stop and start the timer when updating the handles in a callback? EDIT: That kinda worked. If I hit the button once, it doesn't reset. But if I hit the button twice, fast, it works. I suppose I need to wait for the timer to properly stop. There seems to be some timing/function blocking issues here with the callback.
% Reset button
function resetButton_Callback(hObject, eventdata, handles)
disp(handles.count)
handles.count = 0;
stop(handles.timerData);
guidata(hObject,handles);
start(handles.timerData);
end
  1 个评论
Rik
Rik 2019-4-19
It is a bit strange, as Matlab is not really multithreaded. It sounds like you are right: the handles struct is loaded in the two functions at the same time, which means it is a race to the end.
What you could try is using a global to share a lock token between the two functions.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by