Sensor Data Collection with MATLAB Mobile


You can stream sensor data to MATLAB® running in the MathWorks® cloud in real-time with a WiFi or cellular connection. Or, if you don't have a WiFi or cellular connection, you can log data locally and upload later when you do have a connection.

MATLAB Mobile™ includes the command-line interface in MATLAB and the ability to interact with Android™ sensors. To connect to sensors on the device and collect data, you create a mobiledev object in MATLAB.

You can also acquire sensor data locally on the Android device, with or without a network connection. This is an alternative method of collecting the sensor data instead of streaming it from the device to the cloud. This is especially useful if you want to collect sensor data while your device does not have a network connection. For examples of using this method of acquisition, see Log Sensor Data Locally (MATLAB Mobile for Android).

You can also access the camera and acquire images on your Android mobile device from MATLAB Mobile. Set camera properties including resolution, autofocus, and flash. For an example, see Acquire Images from Android Camera (MATLAB Mobile for Android). Then, process the acquired images. For an example, see Classify Images from Android Camera Using Pretrained Network (MATLAB Mobile for Android).

Use MATLAB to process sensor data in various ways, including creating plots. For an example of acquiring data and plotting it, see Use Logged Sensor Data Example (MATLAB Mobile for Android).

Requirements for Using Sensors

To use Android sensors with MATLAB Mobile, make sure you have the following:

  • Installation of MATLAB Mobile on your Android device. You can acquire this application from Google Play™ Store.

  • Android version 6.0 or later.

  • To stream or upload sensor data to the MathWorks cloud, you must have a MathWorks Account. If you do not have an account, you can set one up in MATLAB Mobile.

MATLAB Mobile Sensors Screen

To open the Sensors screen in MATLAB Mobile, tap Sensors on the menu.

Turn the five sensors on and off using their icons at the top of the screen.

The sensor buttons display in this order across the top of the screen, and show these measurements:

  • Acceleration — the three data points are the acceleration reading in X, Y, and Z coordinates, in m/s2 (meters per second squared).

  • Magnetic Field — the three data points are the magnetic field reading in X, Y, and Z coordinates, in microtesla.

  • Orientation — the three data points are the position reading in X, Y, and Z coordinates, in degrees, for azimuth, pitch, and roll.

  • Angular Velocity — the three data points are the angular velocity reading in X, Y, and Z rotations, in radians per second.

  • Position — data points representing latitude, longitude, speed, course, altitude, and horizontal accuracy. Position data is obtained from GPS, Wi-Fi, or the cellular network, using whichever is available. The measurements are:

    • Latitude in degrees relative to the equator, with positive values indicating latitudes north of the equator

    • Longitude in degrees relative to the zero meridian, with positive values extending east of the meridian

    • Speed in meters per second

    • Course in degrees relative to true north

    • Altitude in meters above sea level

    • Horizontal Accuracy in meters defined by a circle around the latitude and longitude

When you tap a sensor, the measurement readings begin and are displayed below the buttons in the order in which you select the sensors. In this example, Acceleration and Orientation have been selected.

After you have selected one or more sensors, use the Start button to send the data to MATLAB on the MathWorks cloud. See Stream Sensor Data with Sensor Controls (MATLAB Mobile for Android).

You can also start the transmission of data from MATLAB programmatically using the Logging property of the mobiledev object. See Stream Sensor Data with mobiledev.logging Property (MATLAB Mobile for Android).

Sensor Settings

The sensor settings on the device are on the Settings screen of MATLAB Mobile. To access them, tap the Menu button, then Settings. Scroll to the Sensors Settings section.

The options are:

  • Configure — Configure MATLAB Mobile sensor settings.

  • Getting Started with Sensors — Access Getting Started information for sensors.

Tap Configure to display the Sensor Settings screen.

You can configure the following settings for sensor data collection:

  • Sensors — Controls MATLAB access to the sensors. The access is on by default and needs to remain on in order to control the sensors and data streaming from MATLAB.

  • Camera — Controls MATLAB access to the cameras. The access is on by default and needs to remain on in order to control camera input from MATLAB.

  • Acquire Data in Background — This setting is off by default, but you can turn it on to continue to send sensor data when the application goes into the background on the device.

  • Upload Folder — The destination folder for uploading log files. The default folder is MATLAB Drive/MobileSensorData. You can change the destination folder in this setting.

  • Auto Upload — This setting determines how data log files are uploaded to MATLAB Drive™.

    • Enabled — If Auto Upload is on, and you have a network connection on your device, log files are automatically uploaded to MATLAB Drive. If Auto Upload is on and you do not currently have a network connection, the files will automatically upload the next time the device detects a network connection (WiFi or cellular, if selected).

    • Disabled — If Auto Upload is off, you must manually upload the file to your MATLAB Drive. See Step 2. Transfer Sensor Logs (MATLAB Mobile for Android).


    If you are using Wi-Fi as your connection method, the Wi-Fi on your device may have a sleep setting that you should adjust.

Device Orientation

The Acceleration, Angular Velocity, Magnetic Field, and Orientation sensors all log data in relation to the X, Y and Z axes. This diagram shows these axes relative to the device.

Note that the Z axis is a positive value when the device is lying flat on a surface. This is by design and is consistent with MATLAB conventions. When the device lies flat on a surface, the acceleration value along the Z axis is +9.81, which corresponds to the acceleration of the device (0 m/s^2) minus the acceleration of gravity (-9.81 m/s^2).

For more information on orientation for position, see the Android documentation.

Related Topics