explain the logic please
2 ビュー (過去 30 日間)
古いコメントを表示
Hi
Here is a small piece of code (3rd party)
*************************************************
function A = TR1_4_CountTime(DCA,Time)
% calculate time differences between each row
for i=1:length(DCA)-1
A(i) = 0 ;
for j=DCA(i)+1:DCA(i+1)
A(i) = A(i) + Count(Time{j},Time{j-1}) ;
end
end
end
function s = Count(time1,time2)
if ~isequal(time1(1:2),time2(1:2)) % if data are not in the same day,
% set default time gap as 1 millisecond
t1=datenum(time1(find(time1==',')+1:end),'HH:MM:SS')+1;
t2=datenum(time2(find(time2==',')+1:end),'HH:MM:SS');
s=round((t1-t2)*24*60); % for minute data
return ;
else % if data are in the same day, calculate time difference
t1=datenum(time1(find(time1==',')+1:end),'HH:MM:SS');
t2=datenum(time2(find(time2==',')+1:end),'HH:MM:SS');
s=round((t1-t2)*24*60); % for minute data
end
end
************************************************************
please can somebody explain what the author is trying to do ?
Bob M
0 件のコメント
採用された回答
Walter Roberson
2021 年 10 月 30 日
編集済み: Walter Roberson
2021 年 10 月 30 日
time1 and time2 are character vectors in which day number are represented as the first two characters . At some point in the character vectors, there is a comma, which is immediately followed by characters representing hours (24 hour), minutes, and seconds.
The code extracts the hours, minutes, and seconds of the two times, and finds the difference and converts it to minutes. If the two times were on different day numbers, then then 1 day is added -- even if the dates were multiple days apart.
I would recommend replacing the whole thing with converting to datetime objects and taking round(minutes()) of the difference of the datetime objects.
0 件のコメント
その他の回答 (1 件)
Bob Matthews
2021 年 10 月 30 日
編集済み: Bob Matthews
2021 年 10 月 30 日
2 件のコメント
Walter Roberson
2021 年 10 月 30 日
In the extract you posted, as outside observers we do not have enough information to know what order the date/times were extacted from the file and stored into the Time array, and we also do not know what DCA is about.
参考
カテゴリ
Help Center および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!