Is there a function that tells me the elapsed time as the elapsed time continues to run?

18 views (last 30 days)
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?

Answers (3)

Walter Roberson
Walter Roberson on 15 Jun 2022
tic and tic have to do with computation time. You have two possibilities:
  • you can use etime()
  • you can datetime('now') and subtract from the start datetime to get a duration()

Steven Lord
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
The time since the initial tic() is 1.00524 seconds. The time since the initial tic() is 2.03409 seconds. The time since the initial tic() is 3.05172 seconds. The time since the initial tic() is 4.05418 seconds. The time since the initial tic() is 5.05817 seconds.
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
The time is 1.00156 seconds. The time is 2.00378 seconds. The time is 3.00593 seconds. The time is 4.00917 seconds. The time is 5.01247 seconds.

Voss
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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!