Contenuto principale

convertTimestamps

Convert timestamp format of sensor data object

Since R2025b

Description

convertTimestamps(sensorDataObj,formatType) converts and updates the timestamp format of the sensor data object sensorDataObj to the specified format formatType.

example

convertTimestamps(sensorDataObj,formatType,StartFrom=startFrom) converts and updates the timestamp format of the sensor data object sensorDataObj to the specified format formatType using the reference start time startFrom.

Note

This feature requires the Scenario Builder for Automated Driving Toolbox™ support package. You can install the Scenario Builder for Automated Driving Toolbox support package from the Add-On Explorer. For more information about installing add-ons, see Get and Manage Add-Ons.

example

Examples

collapse all

Load recorded GPS data into the workspace.

load("recordedGPSData.mat")

Initialize a GPSData object using the information from the GPS data.

gpsData = scenariobuilder.GPSData(timestamps,latitudes,longitudes,altitudes,Name="GPSSensor");

Display the first few timestamps of the GPS data object. Observe that gpsData.Timestamps is a numeric column vector.

head(gpsData.Timestamps)
   1.0e+09 *

    1.4616
    1.4616
    1.4616
    1.4616
    1.4616
    1.4616
    1.4616
    1.4616

Convert the timestamps of the GPS data to a duration array.

convertTimestamps(gpsData,"duration")

Display the first few timestamps of the GPS data object. Observe that gpsData.Timestamps is a duration array.

head(gpsData.Timestamps)
   1.4616e+09 sec
   1.4616e+09 sec
   1.4616e+09 sec
   1.4616e+09 sec
   1.4616e+09 sec
   1.4616e+09 sec
   1.4616e+09 sec
   1.4616e+09 sec

Convert the timestamps of the GPS data to a datetime array.

convertTimestamps(gpsData,"datetime")

Store the first timestamp of the GPS data object as the reference time.

Display the first few timestamps of the GPS data object. Observe that gpsData.Timestamps is a datetime array.

refTime = gpsData.Timestamps(1);
head(gpsData.Timestamps)
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:30

Convert the timestamps of the GPS data to a numeric vector.

convertTimestamps(gpsData,"numeric")

Display the first few timestamps of the GPS data object. Observe that gpsData.Timestamps is a numeric column vector, identical to the timestamps in the raw data.

head(gpsData.Timestamps)
   1.0e+09 *

    1.4616
    1.4616
    1.4616
    1.4616
    1.4616
    1.4616
    1.4616
    1.4616

Normalize the timestamps.

normalizeTimestamps(gpsData)

Display the first few timestamps of the GPS data object. Observe that gpsData.Timestamps starts from 0.

head(gpsData.Timestamps)
         0
    0.0498
    0.0996
    0.1497
    0.1995
    0.2495
    0.2995
    0.3505

Convert the timestamps of the GPS data to a datetime array.

convertTimestamps(gpsData,"datetime")

Display the first few timestamps of the GPS data object. Observe that gpsData.Timestamps does not starts from the reference time refTime.

head(gpsData.Timestamps)
   01-Jan-1970 00:00:00
   01-Jan-1970 00:00:00
   01-Jan-1970 00:00:00
   01-Jan-1970 00:00:00
   01-Jan-1970 00:00:00
   01-Jan-1970 00:00:00
   01-Jan-1970 00:00:00
   01-Jan-1970 00:00:00

Convert the timestamps of the GPS data to a numeric vector, and then to a datetime array with a specified start time.

convertTimestamps(gpsData,"numeric")
convertTimestamps(gpsData,"datetime",StartFrom=refTime)

Display the first few timestamps of the GPS data object. Observe that gpsData.Timestamps is a datetime array with a start time of refTime.

head(gpsData.Timestamps)
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:29
   26-Apr-2016 01:31:30

Input Arguments

collapse all

Sensor data, specified as a GPSData object, Trajectory object, CameraData object, LidarData object, or an ActorTrackData object.

Format to which to convert timestamps, specified as one of these values:

  • "numeric" — Converts and updates the format of the current timestamps of the sensor data object to a numeric array.

  • "duration" — Converts and updates the format of the current timestamps of the sensor data object to a duration array.

  • "datetime" — Converts and updates the format of the current timestamps of the sensor data object to a datetime array.

Note

To specify the startFrom argument, the current format of the timestamps, or the format to which you are converting the timestamps, must be datetime.

Start time for the timestamps, specified as "posix", a datetime object, or a string scalar of the form "dd-MMM-uuuu HH:mm:ss.SSS".

To specify the startFrom argument, the current format of the timestamps, or the format to which you are converting the timestamps, must be datetime.

If you specify startFrom as a string scalar, it must be of the form "dd-MMM-uuuu HH:mm:ss.SSS", where dd is the day, MMM is the abbreviation for the month, uuuu is the year, and HH:mm:ss.SSS is the time in hours, minutes, seconds, and thousandths of a second, respectively. For example, "26-Apr-2016 01:31:29.000". For more information on the date and time format, see the Format property of the datetime data type.

Tips

  • If you want to convert your timestamps to datetime format from a format other than datetime format, and you know the reference time for your initial timestamp, first use the normalizeTimestamps object function to normalize the timestamps to start from 0, and then use the convertTimestamps object function to convert the timestamps to a datetime array, specifying the startFrom argument as the reference time for your initial timestamp. For more information, see the Convert Timestamp Format of GPS Data Object example.

Version History

Introduced in R2025b