Main Content

Stream Sensor Data with MATLAB Object Properties

This example shows how to stream mobile device data to MATLAB® running on the MathWorks® Cloud or your computer with a WiFi or cellular connection. To communicate with the mobile device and acquire data from the sensors, you create the mobiledev object in MATLAB. You cannot log the sensor data until you create the object.

Enable Sensor Data Streaming

Sign in to your MathWorks Cloud, if prompted.

  1. Place the device where you want to acquire the sensor data.

  2. In MATLAB, create a mobiledev object, m.

    m = mobiledev
    mobiledev with properties:
    
                       Connected: 1
               Available Cameras: {'back' 'front'}
                         Logging: 0
                InitialTimestamp: ''
    
       AccelerationSensorEnabled: 1
    AngularVelocitySensorEnabled: 1
           MagneticSensorEnabled: 1
        OrientationSensorEnabled: 1
           PositionSensorEnabled: 1
    
    Supported functions

    In the display that is shown when you create the object:

    • 0 - Not enabled, off, or the sensor is not available on this device.

    • 1 - Enabled or on.

    In this example, you can see that the device and the mobiledev object are connected, but the sensors are not enabled (all sensor values are 0) and data has not been logged yet (InitialTimestamp property is empty). If your device does not have a particular sensor, that sensor always shows a 0 in the display.

    InitialTimestamp is the timestamp of the first data point received by mobiledev. All other timestamps for all sensors are relative to InitialTimestamp.

  3. Enable one or more sensors.

    m.MagneticSensorEnabled=1

    If you are using the position sensor, turn on Location access for MATLAB in Settings on your device.

  4. You can begin logging data from the selected sensors by enabling the Logging property of the mobiledev object.

    m.Logging = 1

    This action starts transmission of data from all selected sensors.

    After you start logging data, allow 1 second for data acquisition, then view the object to see the latest sensor values.

    pause(1)
    m
    mobiledev with properties:
    
                       Connected: 1
               Available Cameras: {'back' 'front'}
                         Logging: 1
                InitialTimestamp: '06-08-2014 13:45:56.529'
    
       AccelerationSensorEnabled: 1
    AngularVelocitySensorEnabled: 1
           MagneticSensorEnabled: 1
        OrientationSensorEnabled: 1
           PositionSensorEnabled: 1
    
    Current Sensor Values:
                    Acceleration: [0.27 0.23 -10.19]  (m/s^2)
                 AngularVelocity: [-0.22 0.07 0.06]   (rad/s)
                   MagneticField: [3.56 1.56 -48.19]  (microtesla)
                     Orientation: [85.91 -27.1 0.35]  (degrees)
    
           Position Data: 
                        Latitude: 41.29  (degrees)
                       Longitude: -72.35  (degrees)
                           Speed: 25  (m/s)
                          Course: 83.6  (degrees)
                        Altitude: 200.1  (m)
              HorizontalAccuracy: 9.0  (m)
  5. In this display, you can see that the device and the Cloud are connected, and data is now being logged. Also, the InitialTimestamp property value and the sensor values are displayed, indicating the current measurement value when you created the object.

  6. Use mobiledev properties to control device sensor acquisition.

  7. To stop streaming sensor data, reset the Logging property or delete the mobiledev object.

    clear m

Control Sensor Acquisition

MATLAB Mobile™ includes properties that display current sensor values, enable and disable sensors, turn logging on and off, set sample rate, and show connection and logging status.

You can use the following properties with the mobiledev object.

PropertyDescription and Values
Connected

Show the status of the connection between MATLAB Mobile and the mobiledev object in MATLAB. Read-only. Possible values are 0 for not connected and 1 for connected. To display the status of connection for a mobiledev object m:

m.Connected

Logging

Show and control the status of data transfer from the device to MATLAB. Possible values are 0 for not logging and 1 for logging. To display the status of logging for a mobiledev object m:

m.Logging

To turn logging on or off:

m.Logging = 1 or m.Logging = 0

InitialTimestamp

Time in milliseconds, in dd-mm-yyyy HH:MM:SS.FFF format, when the first data point was transferred from the device to the mobiledev object. To display the initial timestamp for a mobiledev object m:

m.InitialTimestamp

SampleRate

Sample rate of the acquisition from your device, in Hz. The value is a double and must be in the range of 0 to 100 Hz. The default sample rate is 10 Hz. In addition to setting a specific numeric value, you can also use the character vector values 'high', 'medium', and 'low', which have the following values:

high: 100 Hz

medium: 10 Hz

low: 1 Hz

Acceleration

Current acceleration reading in X, Y, and Z coordinates, in m/s^2 (meters per second squared).

Acceleration is defined in relation to the X, Y and Z axes. If you place the device face-up on a table, the positive X-axis extends out of the right side of the device, positive Y-axis extends out of the top side, and the positive Z-axis extends out of the front face of the device. This is independent of the orientation of the device.

AngularVelocity

Current angular velocity reading in X, Y, and Z rotations, in radians per second.

Angular Velocity is defined in relation to the X, Y and Z axes and in standard right-hand rotational vector notation. If you place the device face-up on a table, the positive X-axis extends out of the right side of the device, positive Y-axis extends out of the top side, and the positive Z-axis extends out of the front face of the device. This is independent of the orientation of the device.

Orientation

Current orientation reading in X, Y, and Z coordinates, in degrees, for azimuth, pitch, and roll.

Orientation is defined in relation to the X, Y and Z axes. If you place the device face-up on a table, the positive X-axis extends out of the right side of the device, positive Y-axis extends out of the top side, and the positive Z-axis extends out of the front face of the device. This is independent of the orientation of the device.

Azimuth is the angle between the positive Y-axis and magnetic north, and its range is between -180 and 180 degrees.

Positive Roll is defined when the device is lying flat on a surface and the positive Z-axis tilts towards the positive X-axis, and its range is between -180 and 180 degrees.

Positive Pitch is defined when the device is lying flat on a surface and the positive Z-axis tilts towards the positive Y-axis, and its range is between -90 and 90 degrees.

MagneticField

Current magnetic field reading in X, Y, and Z coordinates, in microtesla.

Magnetic field is defined in relation to the X, Y and Z axes. If you place the device face-up on a table, the positive X-axis extends out of the right side of the device, positive Y-axis extends out of the top side, and the positive Z-axis extends out of the front face of the device. This is independent of the orientation of the device.

Latitude

Current latitude reading, in degrees relative to the equator, with positive values indicating latitudes north of the equator.

Longitude

Current longitude reading, in degrees relative to the zero meridian, with positive values extending east of the meridian.

HorizontalAccuracy

Current horizontal accuracy, in meters defined by a circle around the latitude and longitude.

Altitude

Current altitude reading, in meters above sea level.

Course

Current course reading, in degrees relative to true north.

Speed

Current speed reading, in m/s (meters per second).

AccelerationSensorEnabled

Turn Acceleration sensor on or off.

To turn sensor on or off:

m.AccelerationSensorEnabled = 1

m.AccelerationSensorEnabled = 0

AngularVelocitySensorEnabled

Turn Angular Velocity sensor on or off.

To turn sensor on or off:

m.AngularVelocitySensorEnabled = 1

m.AngularVelocitySensorEnabled = 0

MagneticSensorEnabled

Turn Magnetic Field sensor on or off.

To turn sensor on or off:

m.MagneticSensorEnabled = 1

m.MagneticSensorEnabled = 0

OrientationSensorEnabled

Turn Orientation sensor on or off.

To turn sensor on or off:

m.OrientationSensorEnabled = 1

m.OrientationSensorEnabled = 0

PositionSensorEnabled

Turn Position sensor on or off.

To turn sensor on or off:

m.PositionSensorEnabled = 1

m.PositionSensorEnabled = 0

Object Properties

The Connected property is read-only and shows the status of the connection between MATLAB Mobile and the mobiledev object in MATLAB. Possible values are 0 for not connected and 1 for connected.

To display the status of the connection, for a mobiledev object m:

m.Connected

The Logging property shows the status of data transfer from the device to MATLAB. Possible values are 0 for not logging and 1 for logging.

To display the status of logging, for a mobiledev object m:

m.Logging

The Logging property is also used to enable and disable logging. To begin logging data from the selected sensors, enable the property.

m.Logging = 1

This action starts the transmitting of data from all selected sensors. You can also start transmission by tapping the Start button in MATLAB Mobile.

You stop logging data from the selected sensors by disabling the Logging property.

m.Logging = 0

This action stops the transmitting of data from all selected sensors. You can also stop transmission by tapping the Stop button in MATLAB Mobile.

Sensor Reading Properties

Use the sensor reading properties to display the current reading of the sensor. At any time you can display the current reading of any of the sensors that are being logged. The Acceleration, AngularVelocity, Orientation, and MagneticField properties display the current readings from their respective sensors. If the Position sensor is logging, you can get individual position readings using the Latitude, Longitude, Speed, Course, Altitude, and HorizontalAccuracy properties.

To get the current value from a sensor, use <objectname>.<propertyname>. For example, to get the acceleration reading, for object m:

m.Acceleration

To get the longitude reading from the Position sensor:

m.Longitude

See Device Orientation for a diagram showing the X, Y, and Z axes relative to the device.

Sensor Control Properties

MATLAB Mobile has five properties to control the sensors. You can perform the same actions by selecting or deselecting the sensor buttons in MATLAB Mobile. Each control property has two possible values: 1 for on or enabled, and 0 for off or disabled.

For example, to turn the Acceleration sensor on:

m.AccelerationSensorEnabled = 1

To turn the Acceleration sensor off:

m.AccelerationSensorEnabled = 0

Sample Rate Property

This property is the sample rate of the data acquisition from your device, in Hz. The value is a double and must be a positive number in the range of 0 to 100 Hz. The default sample rate is 10 Hz.

In addition to setting a specific numeric value, you can also use the character vector values 'high', 'medium', and 'low'. These settings use the following values:

  • high: 100 Hz

  • medium: 10 Hz

  • low: 1 Hz

You can change the sample rate even after you start logging data. For example, to change the sample rate to a specific value in Hz for mobiledev object m:

m.SampleRate = 100

To change the sample rate to one of the character vector values:

m.SampleRate = 'low'

Starting in R2016a, you can use the character vector values or specify a numeric value.

Related Topics