Hello,
I am using a timer function to update texts on pushbuttons in my GUI. After the period of 5 seconds, the timer callback updates the text on the buttons from a matrix. I would like to go to the next loop or queue when I press any button. What should happen is that when I press a button the text on it updates along with resetting the countdown of 5 seconds. Is this possible?

 채택된 답변

Geoff Hayes
Geoff Hayes 2016년 4월 2일

0 개 추천

Justin - yes, it is possible to force an update and reset the timer when the user presses any button. You just need to stop the timer, call the function to update the button text, and then restart the timer.
I noticed that all of your button callbacks are identical with the exception of setting the btn_nbr. You can create one callback that is shared amongst all push buttons by setting the Tag property of each button as
Button1 = uicontrol('Style','pushbutton','String','',...
'Position',[146, 313, 112,27],...
'Callback',@pushButtonCallback, 'Tag','1');
Button2 = uicontrol('Style','pushbutton','String','',...
'Position',[303, 313, 112,27],...
'Callback',@pushButtonCallback, 'Tag','2');
Button3 = uicontrol('Style','pushbutton','String','',...
'Position',[437, 313, 112,27],...
'Callback',@pushButtonCallback, 'Tag','3');
Button4 = uicontrol('Style','pushbutton','String','',...
'Position',[146, 245, 112,27],...
'Callback',@pushButtonCallback, 'Tag','4');
Button5 = uicontrol('Style','pushbutton','String','',...
'Position',[303, 245, 112,27],...
'Callback',@pushButtonCallback, 'Tag','5');
Button6 = uicontrol('Style','pushbutton','String','',...
'Position',[437, 245, 112,27],...
'Callback',@pushButtonCallback, 'Tag','6');
Now all pushbuttons use the same callback so we just need to update pushButtonCallback to handle the new requirements as
function pushButtonCallback(hObject,eventdata)
stop(t); % stop the timer
btn_nbr = str2double(get(hObject,'Tag')); % get the button id
pushed = 1;
updateButtonText; % update the button text
start(t); % restart the timer
end
The updateButtonText is just the body of your TimerFun callback. I did it this way because I didn't want to explicitly call the TimerFun in the above pushbutton callback. Instead the business logic has been put into updateButtonText for both functions, with
function TimerFun(hTimer,eventdata)
updateButtonText;
end
declared as above. See the attached for an updated version of your GUI.m. (Also, btn_nbr doesn't need to be declared as global.)

댓글 수: 1

Justin
Justin 2016년 4월 2일
Thank you. This is exactly what I needed. I tried to use one callback for all the buttons, but I didn't realize I can use tags. I tried to set values to all of them (1,2,3,4,5, and 6), but they can only be true(1) or false(0). Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2016년 4월 1일

댓글:

2016년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by