explain the logic please
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bob Matthews
am 30 Okt. 2021
Bearbeitet: Bob Matthews
am 30 Okt. 2021
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 30 Okt. 2021
Bearbeitet: Walter Roberson
am 30 Okt. 2021
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 Kommentare
Weitere Antworten (1)
Bob Matthews
am 30 Okt. 2021
Bearbeitet: Bob Matthews
am 30 Okt. 2021
2 Kommentare
Walter Roberson
am 30 Okt. 2021
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.
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!