Main Content

Stream Sensor Data with Mobile Device Controls

This example shows how to send mobile device sensor data to MATLAB® running on MathWorks® Cloud or your computer using MATLAB Mobile™ sensor controls. To communicate with the mobile device and acquire data from the sensors, you create a mobiledev object in MATLAB as part of this procedure. You cannot log the sensor data until you create the object.

Sign in to your MathWorks Account, if prompted.

  1. In the Command Window, create a mobiledev object in MATLAB; for example:

    m = mobiledev
    m = 
    mobiledev with properties:
    
                       Connected: 1
               Available Cameras: {'back' 'front'}
                         Logging: 0
                InitialTimestamp: ''
    
       AccelerationSensorEnabled: 0
    AngularVelocitySensorEnabled: 0
           MagneticSensorEnabled: 0
        OrientationSensorEnabled: 0
           PositionSensorEnabled: 0
    
    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.

  2. Tap menu and then tap Sensors.

  3. Turn on the sensor you want using the toggle switch.

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

  4. If not already selected, tap Stream to and select MATLAB.

  5. To start sending data to MATLAB, tap START.

  6. At the MATLAB command line, view sensor data using the mobiledev object; for example:

    m
    Current Sensor Values:
                    Acceleration: [-0.7200 4.6491 8.3620] (m/s^2)
  7. To stop sending data, tap STOP.

Repeat these steps to collect more data.

You can query the latest values and plot the data, or do other post-processing tasks. For examples, see Sensor Data Streaming and Process Logged Sensor Data.

Note

If you are using Wi-Fi® as your connection method, you may have to adjust the sleep setting on your mobile device.

Related Topics