Is it better to use tic/toc or a timer function for tracking time?
18 views (last 30 days)
Show older comments
I am using MATLAB App Designer to make a GUI to read data coming in from an arduino. I want to assign timestamps to incoming data to use for graphing it live, and I am wondering whether it is better to use a timer function or tic/toc? I am mostly asking for performance reasons, it's a rather complex application and I don't want to add any more execution time or whatnot that I can avoid.
0 Comments
Accepted Answer
Steven Lord
on 10 Feb 2025
If you want to retrieve the current time, use:
dt = datetime('now')
To retrieve it as text data:
s = string(dt)
Depending on how you want the time represented (Different time zone? Different display format?) you may need to add other options from the datetime documentation page.
dt = datetime('now', TimeZone="America/New_York")
dt = datetime('now', Format="dd/MM/yy hh:mm:ss a")
The tic and toc functions are for measuring elapsed time, and timer schedules when commands should be executed.
3 Comments
Voss
on 10 Feb 2025
Note that elapsed time can be found by taking the difference of two datetimes, which is a duration (which is suitable for graphing/plotting).
t_start = datetime('now')
pause(3)
t_now = datetime('now')
delta_t = t_now - t_start
Steven Lord
on 10 Feb 2025
t_start = datetime('today')
t_now = datetime('now')
delta_t = t_now - t_start
fprintf("It is now %g seconds or %g minutes or %g hours since midnight.", ...
seconds(delta_t), minutes(delta_t), hours(delta_t))
[h, m, s] = hms(delta_t);
fprintf("It has been %d hours, %d minutes, and %g seconds since midnight.", ...
h, m, s)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!