How to format time with fprintf
45 次查看(过去 30 天)
显示 更早的评论
To get the time it takes a program to execute, I set date_1 = datetime('now") at the beginning of a program, and date_2 = datetime('now) at the end of the program. Then Elapsed_time = date_2 - date_1. I would like to print the elapsed time using the fprintf command, and I would like it to be formatted to show minutes and seconds. How do I do that? Could you provide an example?
0 个评论
采纳的回答
Star Strider
2022-2-28
Try this —
date_1 = datetime('now');
for k = 1:1E+8
rand;
end
date_2 = datetime('now');
Elapsed_time = date_2 - date_1;
Elapsed_time.Format = 'mm:ss.SSS';
fprintf('Elapsed time = %s', Elapsed_time)
I added the milliseconds simply for the demonstration.
.
4 个评论
更多回答(1 个)
Walter Roberson
2022-2-28
date_2 = datetime('now');
date_1 = date_2 - rand()^2 %days
Elapsed_time = date_2 - date_1
Elapsed_time.Format = 'mm:ss'
fprintf('elapsed time was: %s\n', Elapsed_time)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!