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.
Place the device where you want to acquire the sensor data.
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 are0
) and data has not been logged yet (InitialTimestamp
property is empty). If your device does not have a particular sensor, that sensor always shows a0
in the display.InitialTimestamp
is the timestamp of the first data point received bymobiledev
. All other timestamps for all sensors are relative toInitialTimestamp
.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.
You can begin logging data from the selected sensors by enabling the
Logging
property of themobiledev
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)
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.Use
mobiledev
properties to control device sensor acquisition.To stop streaming sensor data, reset the
Logging
property or delete themobiledev
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.
Property | Description and Values |
---|---|
Connected | Show the status of the connection between MATLAB
Mobile and the
|
Logging | Show and control the status of data transfer from the
device to MATLAB. Possible values are
To turn logging on or off:
|
InitialTimestamp | Time in milliseconds, in
|
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
|
Acceleration | Current acceleration reading in Acceleration
is defined in relation to the |
AngularVelocity | Current angular velocity reading in Angular Velocity is defined in
relation to the |
Orientation
| Current orientation reading in Orientation is defined in relation to the
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 Magnetic field is defined in relation
to the |
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:
|
AngularVelocitySensorEnabled | Turn Angular Velocity sensor on or off. To turn sensor on or off:
|
MagneticSensorEnabled | Turn Magnetic Field sensor on or off. To turn sensor on or off:
|
OrientationSensorEnabled | Turn Orientation sensor on or off. To turn sensor on or off:
|
PositionSensorEnabled | Turn Position sensor on or off. To turn sensor on or off:
|
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 Hzmedium
: 10 Hzlow
: 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.