2017-01-09 5 views
1

GUIDE에서 20 초에서 0 초까지 카운트하는 타이머를 만들려고합니다. 그 동안 사용자는 간단한 작업 (그룹 버튼의 라디오 버튼 클릭)을 수행하고 20 초가 지나면 사용자가 클릭 한 버튼에 따라 메시지가 표시됩니다. 주위를 둘러 보았지만 GUIDE 용 타이머 객체가없는 것 같습니다 (왜 유용하지 않으므로 왜 만들지 않습니까 ??). 그러나 나는 그 아래에 결과를 만들려고 노력했으나 작동하지 않습니다. 나는 MyGUI_OpeningFcnsetappdata을 초기화 :MATLAB GUIDE에서 카운트 다운 타이머를 만드는 방법은 무엇입니까?

% 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) 

, 그것은 my_timer를 호출 TimerFcn 호출합니다. my_timer에 핸들을 전달해야하지만 정확히 어떻게 알지는 못합니다. TimerFcn이 타이머 콜백

set(t, 'TimerFcn', @(s,e)my_timer(hObject, handles)) 

에 필요한 데이터를 전달하는

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) 

답변

2

당신은 당신은 다음

function my_timer(hObject, handles) 
    % Do stuff with handles 
end 
my_timer 콜백을 정의 할 수 있습니다 사용자 정의 익명 함수를 사용할 필요가
관련 문제