How do I implement a timer function that updates a static text box within my GUI?
2 views (last 30 days)
Show older comments
Austin Hoggatt
on 4 Apr 2018
Answered: Sean de Wolski
on 4 Apr 2018
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);
0 Comments
Accepted Answer
Sean de Wolski
on 4 Apr 2018
You need to pass handles into the callback
@(t,e)timerCallback(t,e,handles)
Also, why aren't you using app designer?
doc appdesigner
0 Comments
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!