The way we call ros::Duration in C++, I need to call in Matlab. What its equivalent expression in MATLAB?
MATLAB: What is Matlab equivalent of C++ ros::Duration()
cRobotics System Toolboxros
Related Solutions
What do you mean by "number of seconds" ? The number of seconds from when? From what epoch? (without an epoch it is meaningless to count seconds).
The definition of MATLAB's serial date number is clearly defined: "the number of days from January 0, 0000". If you want this time in seconds then simply multiply the serial date number by the number of seconds in a day. Note that these numbers will be much larger!
If you only need the seconds component of those timestamps, then use datevec, and take a look at the sixth column of data:
>> C = {'2013-01-01 00:00:00' '2013-01-01 00:00:01' '2013-01-01 00:00:02'};>> V = datevec(C)V = 2013 1 1 0 0 0 2013 1 1 0 0 1 2013 1 1 0 0 2>> V(:,6)ans = 0 1 2
You can do this in several ways, but honestly the most straight-forward is to write a loop, as shown below. The real question is, why do you want to do this? I'm genuinely asking, because there are tools that let you do things like hourly means without splitting the data up. I guess your answer would be, "I want to plot things separately". Fair enough: you can trick rowfun into doing that but it's a little tricky. Are there other things you want to do on the separate timetables?
Anyways, my solution (which could certainly be tightened up but is at least clear):
>> tt = timetable(rand(15,1),[1;1;1;0;0;1;1;1;1;0;1;1;1;1;1], ... 'RowTimes',datetime(2019,6,19,0,0,0:14), ... 'VariableNames',{'X' 'OnOff'});>> tt.Group = 1 + [0; cumsum(diff([tt.OnOff])~=0)]tt = 15×3 timetable Time X OnOff Group ____________________ ________ _____ _____ 19-Jun-2019 00:00:00 0.69989 1 1 19-Jun-2019 00:00:01 0.63853 1 1 19-Jun-2019 00:00:02 0.033604 1 1 19-Jun-2019 00:00:03 0.068806 0 2 19-Jun-2019 00:00:04 0.3196 0 2 19-Jun-2019 00:00:05 0.53086 1 3 19-Jun-2019 00:00:06 0.65445 1 3 19-Jun-2019 00:00:07 0.40762 1 3 19-Jun-2019 00:00:08 0.81998 1 3 19-Jun-2019 00:00:09 0.71836 0 4 19-Jun-2019 00:00:10 0.96865 1 5 19-Jun-2019 00:00:11 0.53133 1 5 19-Jun-2019 00:00:12 0.32515 1 5 19-Jun-2019 00:00:13 0.10563 1 5 19-Jun-2019 00:00:14 0.61096 1 5 >> c = cell(numel(unique(tt.Group)),1);>> for i = 1:length(c), c{i} = tt(tt.Group == i,1:2); end>> c{:}ans = 3×2 timetable Time X OnOff ____________________ ________ _____ 19-Jun-2019 00:00:00 0.69989 1 19-Jun-2019 00:00:01 0.63853 1 19-Jun-2019 00:00:02 0.033604 1 ans = 2×2 timetable Time X OnOff ____________________ ________ _____ 19-Jun-2019 00:00:03 0.068806 0 19-Jun-2019 00:00:04 0.3196 0 ans = 4×2 timetable Time X OnOff ____________________ _______ _____ 19-Jun-2019 00:00:05 0.53086 1 19-Jun-2019 00:00:06 0.65445 1 19-Jun-2019 00:00:07 0.40762 1 19-Jun-2019 00:00:08 0.81998 1 ans = 1×2 timetable Time X OnOff ____________________ _______ _____ 19-Jun-2019 00:00:09 0.71836 0 ans = 5×2 timetable Time X OnOff ____________________ _______ _____ 19-Jun-2019 00:00:10 0.96865 1 19-Jun-2019 00:00:11 0.53133 1 19-Jun-2019 00:00:12 0.32515 1 19-Jun-2019 00:00:13 0.10563 1 19-Jun-2019 00:00:14 0.61096 1
Best Answer