- you can use etime()
- you can datetime('now') and subtract from the start datetime to get a duration()
Is there a function that tells me the elapsed time as the elapsed time continues to run?
18 views (last 30 days)
Show older comments
I have been working on an analog clock with a stopclock function. My clock runs in a figure and on there I want to put two buttons (one 'start' and one 'stop'). I am using uicontrol and am having a hard time figuring out how to get the clock hands to point to the correct position as the current elapsed time changes. For example, the second clock hand is at its default position (at 12) and will move as soon as the start button is pressed. I know that there are tic and toc functions that tell me the elapsed time but this doesn't work in my case (do they?). Is there a function that can tell me the current elapsed time?
0 Comments
Answers (3)
Walter Roberson
on 15 Jun 2022
tic and tic have to do with computation time. You have two possibilities:
0 Comments
Steven Lord
on 15 Jun 2022
t = tic;
for k = 1:5
pause(1)
fprintf("The time since the initial tic() is %g seconds.\n", toc(t))
end
From the toc documentation page: "toc(timerVal) displays the elapsed time since the call to the tic function corresponding to timerVal." You can also do this by calling tic without an output argument and toc without an input, but this could run into problems if some of the code between those calls themselves call tic and/or toc.
tic
for k = 1:5
pause(1)
fprintf("The time is %g seconds.\n", toc)
end
0 Comments
Voss
on 15 Jun 2022
figure( ...
'Name','Stopwatch', ...
'NumberTitle','off', ...
'Menubar','none', ...
'Toolbar','none', ...
'DockControls','off', ...
'Units','pixels', ...
'Position',[500 600 600 600]);
axes('Visible','off');
r_clock = 0.95;
r_hand = 0.97*r_clock;
r_number = 0.92*r_clock;
r_tick = [0.98 0.96]*r_clock;
th = pi/2-pi/6*(1:12);
text(r_number*cos(th),r_number*sin(th),sprintfc('%d',1:12), ...
'FontWeight','bold', ...
'FontSize',12, ...
'HorizontalAlignment','center', ...
'VerticalAlignment','middle');
th = linspace(0,2*pi,241);
line(r_clock*cos(th),r_clock*sin(th),'Color','k','LineWidth',2);
th = linspace(0,2*pi,61);
xd = cos(th).*[r_clock; r_tick(1)];
xd(end+1,:) = NaN;
yd = sin(th).*[r_clock; r_tick(1)];
yd(end+1,:) = NaN;
line(xd(:),yd(:),'Color','k');
xd = xd(:,1:5:end);
yd = yd(:,1:5:end);
xd(2,:) = xd(2,:)*r_tick(2)/r_tick(1);
yd(2,:) = yd(2,:)*r_tick(2)/r_tick(1);
line(xd(:),yd(:),'Color','k');
time_text = text(0,0,'', ...
'Units','normalized', ...
'HorizontalAlignment','left', ...
'VerticalAlignment','bottom', ...
'EdgeColor','k', ...
'Margin',1, ...
'FontName','FixedWidth');
hand_line = line(NaN,NaN,'Color','r','LineWidth',2);
axis square
t = clock();
toc_t = tic();
while true
time = etime(clock(),t);
toc_time = toc(toc_t);
th = pi/2-2*pi*time/60;
set(hand_line, ...
'XData',[0 r_hand*cos(th)], ...
'YData',[0 r_hand*sin(th)]);
set(time_text, ...
'String',sprintf('etime: %5.2f sec\n toc: %5.2f sec',time,toc_time))
drawnow();
end
0 Comments
See Also
Categories
Find more on Logical 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!