Convert Time in timeseries object from posix to datetime

14 次查看(过去 30 天)
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 个评论
Sylvain
Sylvain 2020-11-5
Your code works fine, so it seems you import NaN data, flagged as error.
Georg Novotny
Georg Novotny 2020-11-5
Hello Sylvian,
thank you for your reply!
I don't think that
datetime(round(1000*GPSts.Time),'ConvertFrom','epochtime','Epoch', '1970-01-01', ...
"TicksPerSecond",1000,'Format','HH:mm:ss.SSSS')
returns NaN.
Please see the output of said command:
ans =
1067×1 datetime array
14:40:37.7580
14:40:38.0080
14:40:38.2490
14:40:38.5020
14:40:38.7510
14:40:38.9980
14:40:39.2510
14:40:39.5090
14:40:39.7480

请先登录,再进行评论。

采纳的回答

Georg Novotny
Georg Novotny 2020-11-6
Here my working code:
clear
close all
Read rosbag and store data in variables
rosbagFolder = 'C:\Users\georg\OneDrive\Desktop\Rosbag';
filePattern = fullfile(rosbagFolder, '*.bag');
rosbagFiles = dir(filePattern);
allMsgsGPS = cell(0,0);
allMsgsLIDAR = cell(0,0);
for i = 1 : length(rosbagFiles)
bag = rosbag(rosbagFiles(i).name);
bagSelGPS = select(bag, 'Time',...
[bag.StartTime bag.EndTime], 'Topic', '/gx5/gps/fix');
MsgsGPS= readMessages(bagSelGPS, 'DataFormat','struct');
allMsgsGPS =[allMsgsGPS; MsgsGPS];
end
Convert data to matlab Mat format
gpsTime = cellfun(@(t) datetime( double(t.Header.Stamp.Sec)+double(t.Header.Stamp.Nsec)*10^-9, ...
'ConvertFrom','posixtime', 'Format', 'HH:mm:ss.SSSS'), allMsgsGPS);
gpsLatitude = cellfun(@(m) double(m.Latitude), allMsgsGPS);
gpsLongitude = cellfun(@(m) double(m.Longitude), allMsgsGPS);
gpsAltitude = cellfun(@(m) double(m.Altitude), allMsgsGPS);
gpsSequence = timetable(gpsTime, gpsLatitude, gpsLongitude, gpsAltitude);
gpsFrameDuration = mean(diff(gpsSequence.Time));
gpsFrameDuration.Format = "s";
gpsRate = 1/seconds(gpsFrameDuration);
fprintf('GPS : %s, %3.1f Hz\n', char(gpsFrameDuration), gpsRate);

更多回答(1 个)

Peter Perkins
Peter Perkins 2020-11-18
编辑:Steven Lord 2020-11-18
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.]

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by