function stopwatch(cmd)
%STOPWATCH Creates a GUI that displays elapsed time from mouse or keyboard inputs
%
% Controls:
% Press the START button to begin the timer (or press any key except L, R, or X)
% If the timer has already been activated, press the PAUSE button to
% stop the timer (or press any key except L, R, or X)
% If the timer has been paused, press the RESUME button to continue from
% the paused time (or press any key except L, R, or X)
% If the timer is in lap mode, press the RESUME button to continue as though
% the lap time had not been activated (or press any key except L, R, or X)
% Press the LAP button to view lap times (or press the L key)
% The LAP button can be pressed successively to view mulitple laps
% Press the RESET button to restore the timer (or press the R key)
% Press the EXIT button to close the timer window (or press the X key)
%
% Example usage: >> STOPWATCH
% AUTHOR: Joseph Kirk (c) 3/2007
% EMAIL: jdkirk630 at gmail dot com
global HFIG TIME DISPLAY START STOPPED LAP LAPFLAG
if nargin < 1
TIME = 0; STOPPED = 1; LAPFLAG = 0;
HFIG = figure('Name', '秒表计时器', 'Numbertitle', 'off', 'Position', [400 300 350 100], ...
'Menubar', 'none', 'Resize', 'off', 'KeyPressFcn', [mfilename, '(''KEY'')']);
filemenu = uimenu('Label', '文件');
uimenu(filemenu, 'Label', '退出', 'Callback', [mfilename, '(''EXIT'')']);
helpmenu = uimenu('Label', '帮助');
uimenu(helpmenu, 'Label', '帮助文件', 'Callback', 'help stopwatch');
uimenu(helpmenu, 'Label', '关于7', 'Separator', 'on', 'Callback', ['msgbox({'' Stopwatch' ...
' GUI'', '' '', '' by Joseph Kirk (c) 2007'', '' jdkirk630@gmail.com''},''About'')']);
START = uicontrol(HFIG, 'Style', 'PushButton', 'Position', [10 10 75 25], ...
'String', '开始', 'Callback', [mfilename, '(''START'')']);
LAP = uicontrol(HFIG, 'Style', 'PushButton', 'Position', [95 10 75 25], ...
'String', '计时 (L)', 'Callback', [mfilename, '(''LAP'')'], 'Enable', 'off');
uicontrol(HFIG, 'Style', 'PushButton', 'Position', [180 10 75 25], ...
'String', '从置 (R)', 'Callback', [mfilename, '(''RESET'')']);
uicontrol(HFIG, 'Style', 'PushButton', 'Position', [265 10 75 25], ...
'String', '退出 (X)', 'Callback', [mfilename, '(''EXIT'')']);
DISPLAY = uicontrol(HFIG, 'Style', 'text', 'Position', [25 45 300 55], ...
'BackgroundColor', [0.8 0.8 0.8], 'FontSize', 35, 'String', '00:00:00.000');
else %----- GUI Callback Commands -----%
switch cmd
case 'START'
if LAPFLAG
t = toc;
tic;
TIME = TIME + t;
else
tic;
end
STOPPED = 0;
set(START, 'String', '暂停', 'Callback', [mfilename, '(''PAUSE'')']);%
set(LAP, 'Enable', 'on');
while ~STOPPED
t = toc; str = format_time(TIME + t); set(DISPLAY, 'String', str);
pause(0.01);
end
case 'LAP'
STOPPED = 1; LAPFLAG = 1; t = toc;
str = format_time(TIME + t); set(DISPLAY, 'String', str);
set(START, 'String', '恢复', 'Callback', [mfilename, '(''START'')']);
case 'PAUSE'
STOPPED = 1; LAPFLAG = 0; t = toc; TIME = TIME + t;
str = format_time(TIME); set(DISPLAY, 'String', str);
set(START, 'String', '恢复', 'Callback', [mfilename, '(''START'')']);
set(LAP, 'Enable', 'off');
case 'RESET'
STOPPED = 1; LAPFLAG = 0; TIME = 0;
str = format_time(TIME); set(DISPLAY, 'String', str);
set(START, 'String', '开始', 'Callback', [mfilename, '(''START'')']);
set(LAP, 'Enable', 'off');
case 'EXIT'
STOPPED = 1;
close(HFIG);
case 'KEY'
switch upper(get(HFIG, 'CurrentCharacter'))
case 'L'
if strcmp(get(LAP, 'Enable'), 'on')
feval(mfilename, 'LAP');
end
case 'R'
feval(mfilename, 'RESET');
case 'X'
feval(mfilename, 'EXIT');
otherwise
eval(get(START, 'Callback'));
end
otherwise
return
end
end
%----- SUBFUNCTION: format_time(...) ---------------------------------------------------------------
function str = format_time(t)
hrs = floor(t/3600);
min = floor(t/60 - 60*hrs);
sec = t - 60*(min + 60*hrs);
if hrs < 10
h = sprintf('0%1.0f:', hrs);
else
h = sprintf('%1.0f:', hrs);
end
if min < 10
m = sprintf('0%1.0f:', min);
else
m = sprintf('%1.0f:', min);
end
if sec < 9.9995
s = sprintf('0%1.3f', sec);
else
s = sprintf('%1.3f', sec);
end
str = [h m s];
评论3