1
我試圖做一個計時器,從20到0(秒)在GUIDE中倒計時。與此同時,用戶將執行一個簡單的操作(單擊組按鈕中的單選按鈕),並在20秒結束時顯示一條消息(取決於用戶點擊哪個按鈕)。 我環顧四周,但似乎沒有GUIDE的定時器對象(爲什麼不製作一個,因爲它非常有用?)。不過,我試圖讓一個和以下的結果,它不起作用。 我MyGUI_OpeningFcn
初始化setappdata
:如何在MATLAB指南中製作倒數計時器?
% Initialize setappdata
timeout = 20;
setappdata(handles.figure1,'timeout', timeout);
Next_calculation
是單選按鈕,timerBox
是一個靜態文本。
function Next_calculation_Callback(hObject, eventdata, handles)
[..]
timeout = getappdata(handles.figure1,'timeout');
t = timer('Period', 1.0,... % 1 second
'StartFcn', set(handles.timerBox,'String',num2str(timeout)), ...
'ExecutionMode', 'fixedRate', ... % Starts immediately after the timer callback function is added to the MATLAB execution queue
'TasksToExecute', timeout, ... % Indicates the number of times the timer object is to execute the TimerFcn callback
'TimerFcn', @my_timer ... % callback to function
);
start(t)
一旦定時器開始,它調用TimerFcn
調用my_timer
。我應該通過my_timer
的句柄,但我不知道如何。
function my_timer(hObject, eventdata)
% I think I'm supposed to pass (hObject, eventdata) to my_timer
% handles should be getting the current figure from hObject
handles = guidata(ancestor(hObject, 'figure1'));
timeout = getappdata(handles.figure1,'timeout');
t_left = timeout - 1.0;
% show the updated time
set(handles.timerBox,'String',num2str(t_left));
% update 'timeout'
setappdata(handles.figure1,'timeout',t_left)