Convert Time in timeseries object from posix to datetime
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Georg Novotny
le 5 Nov 2020
Modifié(e) : Steven Lord
le 18 Nov 2020
Hello,
I want to convert the Time in a timeseries object called GPSts (which currently is posixtime with nanoseconds e.g. "1604328037.75777") to datetime in the format 'HH:mm:ss.SSS' e.g. "23:46:11.4570".
I can convert the posixtime to the desired datetime using:
datetime(round(1000*GPSts.Time),'ConvertFrom','epochtime','Epoch', '1970-01-01', "TicksPerSecond",1000,'Format','HH:mm:ss.SSSS')
but when I try to modify the value of GPSts.Time I get the following errors:
Check for missing argument or incorrect argument data type in call to function 'isnan'.
if any(isinf(time)) || any(isnan(time))
t = timeseries.tsChkTime(t);
this.TimeInfo = reset(this.TimeInfo,input);
Please see my complete code example:
clc
clear
rosbagFolder = 'C:\Users\georg\OneDrive\Desktop\Rosbag';
filePattern = fullfile(rosbagFolder, '*.bag');
rosbagFiles = dir(filePattern);
GPSts = timeseries();
for i = 1 : length(rosbagFiles)
bag = rosbag(rosbagFiles(i).name);
bagSelGPS = select(bag, 'Time',...
[bag.StartTime bag.EndTime], 'Topic', '/gx5/gps/fix');
MsgsGPSTimeseries = timeseries(bagSelGPS, 'Latitude', 'Longitude', 'Altitude');
GPSts = append(GPSts, MsgsGPSTimeseries);
end
GPSts.Time = datetime(round(1000*GPSts.Time),'ConvertFrom','epochtime','Epoch', '1970-01-01', ...
"TicksPerSecond",1000,'Format','HH:mm:ss.SSSS') %Error occurs here
gpsFrameDuration = median(diff(GPSts.Time));
gpsFrameDuration.Format = "s";
gpsRate = 1/seconds(gpsFrameDuration);
2 commentaires
Réponse acceptée
Plus de réponses (1)
Peter Perkins
le 18 Nov 2020
Modifié(e) : Steven Lord
le 18 Nov 2020
Georg, three suggestions:
1) Numeric precision may be better if you separate the conversion of whole and fractional seconds like this:
datetime(double(t.Header.Stamp.Sec), 'ConvertFrom','posixtime', 'Format', 'HH:mm:ss.SSSS') + milliseconds(+double(t.Header.Stamp.Nsec)*10^-6)
2) Setting the datetime format to 'HH:mm:ss.SSSS' suggests that maybe you could be using durations, not datetimes. If what you want is "time since midnight", you can get that with the timeofday function.
3) I'm not actually very familiar with ROS or the ROS Toolbox, and if your code works, that's great, but if allMsgsGPS is a cell array of scalar structs all with the same fields, each contianing a scalar string, you might consider something like the following, which is likely to be more performant than what you have:
>> s1.a = "1"; s1.b = "4";
>> s2.a = "2"; s2.b = "5";
>> s3.a = "3"; s3.b = "6";
>> c = {s1;s2;s3}
c =
3×1 cell array
{1×1 struct}
{1×1 struct}
{1×1 struct}
>> s = vertcat(c{:})
s =
3×1 struct array with fields:
a
b
>> a = double([s.a])';
>> b = double([s.b])';
>> t = table(a,b)
t =
3×2 table
a b
_ _
1 4
2 5
3 6
[SL: added missing ' in the datetime call.]
0 commentaires
Voir également
Catégories
En savoir plus sur ROS Log Files and Transformations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!