MATLAB: Convert Time in timeseries object from posix to datetime

epochtime convert to datetimerosbagtimeseries

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);

Best Answer

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);