MATLAB tic-toc以Minutes格式生成

问题描述 投票:12回答:3

我在我的Matlab项目中使用tic-toc函数的地方很多。输出时间可以是331.5264 or 1234.754 seconds等。我可以输出这是分钟格式吗?例如。 5 minutes and 30.6 seconds?谢谢!

matlab timer
3个回答
20
投票

您所要做的就是从toc捕获输出(而不是让它显示其默认输出),然后使用函数fprintffloorrem自行创建输出:

 tStart = tic;
 % Do some things...
 tEnd = toc(tStart);
 fprintf('%d minutes and %f seconds\n', floor(tEnd/60), rem(tEnd,60));

5
投票

虽然tic和toc没有任何方法可以在几分钟内显示值,但您可以在显示之前稍微处理数据。查看以下link到秒到小时/分钟的转换器。

用法如下:

tic  
% Do something
time_str = SECS2HMS(toc)
disp(time_str)

当我回到我的Windows VM时,我会尝试这个。希望这可以帮助。

编辑 您可以使用Matlab中内置的datestr和datenum函数以及以下方式。请注意,我还没有尝试过这段代码,但链接让我想起了如何做到这一点的语法(没有提出Matlab)

tic
%Do something  
t=toc;
disp(datestr(datenum(0,0,0,0,0,t),'HH:MM:SS'))

2
投票

我发现最简单的方法是使用:

TIME = tic; % do calculations which take TIME to complete fprintf('This took %s', duration([0, 0, toc(TIME)]));

toc() returns the time from the stopwatch in secondsduration() expects a three-column matrix expressing a time duration in [hours, minutes, seconds] format)。

这具有保持大部分时序计算不可见的良好特性。

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.