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];
秒表计时器_matlab_
5星 · 超过95%的资源 39 浏览量
2021-10-03
00:34:46
上传
评论 4
收藏 2KB RAR 举报
食肉库玛
- 粉丝: 57
- 资源: 4740
最新资源
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
- 音乐展示网页、基于Stenography的图像数字水印添加与提取,以及基于颜色矩和Tamura算法的图像相似度评估算法py源码
- 基于EmguCV(OpenCV .net封装),图像数字水印加解密算法的实现,其中包含最低有效位算法,离散傅里叶变换算法+文档书
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论3