Matlab:以小数秒计算日期时间数组之间的差异

Matlab:以小数秒计算日期时间数组之间的差异,matlab,datetime,Matlab,Datetime,我正在以分数精度(百分之一秒)读取时间数据: 对于此datetime数组中的每个条目,我想确定与第一个条目的差异,单位为秒(分数精度): 这将返回: eventtimes = 0h 0m 19.72s 0h 1m 46s 0h 6m 45.9s 0h 6m 53.18s 我想从这里得到一个规则数组,只需保持(分数)秒: 到目前为止,我尝试过的方法(分割、时间)总是会丢失分数。假设eventtimes是字符串的单元格数组,可以执行以下操作: eventtimes={'0h 0m 19.72s

我正在以分数精度(百分之一秒)读取时间数据:

对于此datetime数组中的每个条目,我想确定与第一个条目的差异,单位为秒(分数精度):

这将返回:

eventtimes = 

0h 0m 19.72s
0h 1m 46s
0h 6m 45.9s
0h 6m 53.18s
我想从这里得到一个规则数组,只需保持(分数)秒:


到目前为止,我尝试过的方法(分割、时间)总是会丢失分数。

假设
eventtimes
是字符串的单元格数组,可以执行以下操作:

eventtimes={'0h 0m 19.72s'
    '0h 1m 46s'
    '0h 6m 45.9s'
    '0h 6m 53.18s'};

for i=1:length(eventtimes)
    %// Read each line of data individually
    M(i,:)=sscanf(eventtimes{i},'%d%*s%d%*s%f%*s').';
end

s=(M(:,1)*60+M(:,2))*60+M(:,3) %// Convert into seconds

s =
   19.7200
  106.0000
  405.9000
  413.1800

关于@David提供的答案,最后一行代码:

s=(M(:,1)*24+M(:,2))*60+M(:,3) %// Convert into seconds
应该是:

s=(M(:,1)*60+M(:,2))*60+M(:,3) %// Convert into seconds
由于
M(:,1)
包含
小时
,要将它们转换为秒,必须将它们乘以
3600(60分钟*60秒)

问题中描述的预期结果和@David提供的结果似乎是正确的,因为所有输入都有“
0h

如果任何输入时间超过1h,结果将为:

0h 0m 19.72s
2h 1m 46s
3h 6m 45.9s
0h 6m 53.18s

s=
   19.72
 7306.00
11205.90
  413.18

希望这能有所帮助。

我找到了一个使用etime的解决方案,它绕过了“between”函数及其奇数calendarDuration类型输出:

%read in with fractions
times=datetime(myCellArray(:,1),'InputFormat','HH:mm:ss.SS');

%turn into datevectors (uses today's date: works as long as midnight isn't crossed)
timevect=datevec(times);

%separate start time from subsequent times
starttime=timevect(1,:);
othertimes=timevect(2:end,:);

%use etime with repmat of starttime to get differences:
relativetimes=etime(othertimes,repmat(starttime,[size(othertimes,1) 1]));

relativetimes(1:4)

ans =

                 19.72
                   106
                 405.9
                413.18

eventtimes
是字符串数组吗
between
是一个我们无权访问的函数。@rayryeng:“between”是一个可从(至少)Matlab 2014b:[link]()获得的函数,它产生一个特殊的“datetime”类型的数组(不是字符串数组)。如果有另一种方法可以获得时差(保留分数),我完全支持它。对不起。我是2013a。我也做了谷歌搜索,结果一无所获。。。这就是我发表评论的原因。谢谢请注意,在您的答案中,
M(:,1)
应该乘以
3600
(请参见下面我的答案),而不是乘以
24
哦,是的,小时而不是天!!谢谢不幸的是,eventtimes不是字符串的单元格数组,而是(最近?)的“calendarDuration”类型。这就是为什么它有助于包含运行的完整代码,或者至少解释问题所在,而不是让我们猜测如此重要的事情。感谢您注意到错误,实际上,我没有检查括号,而是用计算器进行乘法运算(我的错)。然而,即使计算括号,将
M(:,1)
乘以
24
似乎也不正确,它必须乘以
60
。我已经在我的回答中更新了代码。我接受你的批评,这是我应得的。我也可以删除我的答案。尽管如此,请至少更新您的答案并修复错误。不幸的是,eventtimes不是字符串的单元格数组,而是“calendarDuration”类型。事实证明,这个变量的解析是一个令人头痛的问题。与此同时,我自己也提出了一个解决方案。。
0h 0m 19.72s
2h 1m 46s
3h 6m 45.9s
0h 6m 53.18s

s=
   19.72
 7306.00
11205.90
  413.18
%read in with fractions
times=datetime(myCellArray(:,1),'InputFormat','HH:mm:ss.SS');

%turn into datevectors (uses today's date: works as long as midnight isn't crossed)
timevect=datevec(times);

%separate start time from subsequent times
starttime=timevect(1,:);
othertimes=timevect(2:end,:);

%use etime with repmat of starttime to get differences:
relativetimes=etime(othertimes,repmat(starttime,[size(othertimes,1) 1]));

relativetimes(1:4)

ans =

                 19.72
                   106
                 405.9
                413.18