How to add a timer
    7 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
All I need is a simple timer that is displayed and counts up while a game is being played.
0 commentaires
Réponses (1)
  Matt Fig
      
      
 le 12 Déc 2012
        
      Modifié(e) : Matt Fig
      
      
 le 12 Déc 2012
  
      Here is a simple example. You should be able to adapt the concepts to your GUI or whatever.
function [] = gui_timer()
% Make a GUI that counts by seconds...
S.fh = figure('name','gui_timer',...
              'menubar','none',...
              'numbert','off',...
              'pos',[100 100 300 150],...
              'closereq',@deleter);
S.tx = uicontrol('Style','text',...
                 'Units','pix',...
                 'Position',[10 10 280 130],... 
                 'fontsize',60,...
                 'String','0',...
                 'backgroundc',get(S.fh,'color'));
S.tmr = timer('Name','TimerCounter',...
              'Period',1,...  %             
              'StartDelay',1,... % In seconds.
              'TasksToExecute',inf,...  % Do until delete
              'ExecutionMode','fixedSpacing',...
              'TimerFcn',@tmr_tmrfcn,...   % Function def. below.
              'StopFcn',@deleter);         % Function def. below.
guidata(S.fh,S)
movegui('center') 
start(S.tmr);
function [] = tmr_tmrfcn(varargin) 
% Callback for timerfcn.
S = guidata(findall(0,'name','gui_timer'));
V = str2double(get(S.tx,'string'));
set(S.tx,'string',sprintf('%i',V+1))
function deleter(varargin)   
% Callback for stopfcn and figure close.
T = timerfind('name','TimerCounter');
stop(T);
delete(T);
delete(findall(0,'name','gui_timer'))
0 commentaires
Voir également
Catégories
				En savoir plus sur Strategy & Logic dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!