MATLAB: How to implement a timer function that updates a static text box within the GUI

countdownguitimer function

My goal is to have a countdown show in a static text box while the user is answering math facts. The timer function as implemented in the code below displays an error of "Not enough input arguments"
Is there a syntax issue with my use of the timer or something more fundamental about how timers work within the GUI that I'm missing to properly execute this task?
function varargout = flashcard_app(varargin)
% FLASHCARD_APP MATLAB code for flashcard_app.fig
% FLASHCARD_APP, by itself, creates a new FLASHCARD_APP or raises the existing
% singleton*.
%



% H = FLASHCARD_APP returns the handle to a new FLASHCARD_APP or the handle to
% the existing singleton*.
%
% FLASHCARD_APP('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FLASHCARD_APP.M with the given input arguments.
%
% FLASHCARD_APP('Property','Value',...) creates a new FLASHCARD_APP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before flashcard_app_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to flashcard_app_OpeningFcn via varargin.
%
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @flashcard_app_OpeningFcn, ...
'gui_OutputFcn', @flashcard_app_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before flashcard_app is made visible.
function flashcard_app_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Initiate timer and callback function:
handles.timer = timer('Period',1,'TimerFcn',@timerCallback);
% Update handles structure
guidata(hObject, handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UIWAIT makes flashcard_app wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = flashcard_app_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in start_button.
function start_button_Callback(hObject, eventdata, handles)
%Begin Timer Function:
set(handles.timer_value,'String','10');
start(handles.timer);
function timerCallback(hObject, eventdata, handles)
disp('Timer Function Executing')
current_timer = get(handles.timer_value,'String');
sprintf('The current timer value is %d', current_timer)
t = str2double(current_timer);
t = t-1;
new_timer = num2str(t);
sprintf('The new timer value is %d', new_timer)
set(handles.timer_value,'String',new_timer)
guidata(hObject,handles);

Best Answer

You need to pass handles into the callback
@(t,e)timerCallback(t,e,handles)
Also, why aren't you using app designer?
doc appdesigner
Related Question