Main Content

distance

Compute total distance traveled for data stored in GPS data object

Since R2024b

Description

traveledDistance = distance(gpsData) returns the total distance traveled, in meters, for data stored in the GPS data object gpsData.

Note

This function 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 by using information from the loaded GPS data.

gpsData = scenariobuilder.GPSData(timestamps,latitudes,longitudes,altitudes)
gpsData = 
  GPSData with properties:

          Name: ''

    NumSamples: 392
      Duration: 19.5498
    SampleRate: 20.0513
    SampleTime: 0.0500
    Timestamps: [392×1 double]

      Latitude: [392×1 single]
     Longitude: [392×1 single]
      Altitude: [392×1 single]

    Attributes: []

Plot the waypoints in the GPS data object.

plot(gpsData)

Compute the total traveled distance, as recorded in the GPS data object. Units are in meters.

traveledDistance = distance(gpsData)
traveledDistance = single

373.3607

Input Arguments

collapse all

GPS data, specified as a GPSData object.

Output Arguments

collapse all

Total distance traveled for data stored in the GPS data object, returned as a scalar. Units are in meters. The function uses the Haversine formula to compute piecewise distance between consecutive world points. For more information, see Haversine Distance.

Data Types: single

Algorithms

The function uses the Haversine formula to compute the distance between two world points specified as (lat1, lon1) and (lat2, lon2). The Haversine distance is calculated as:

distance = R*2*atan2(sqrt(A),sqrt(1-A)),

where, the value of R is 6,378,137 meters, which specifies the radius of the Earth, and A is computed as:

A=sin(lat2lat12)2+cos(lat1)×cos(lat2)×sin(lon2lon12)2

Version History

Introduced in R2024b