Android Ug
Android Ug
Devices
User’s Guide
R2021a
How to Contact MathWorks
Phone: 508-647-7000
DSP System Toolbox Array Plot Block and Mobile Apps . . . . . . . . . . . . . 1-36
Add DSP System Toolbox Array Plot to App . . . . . . . . . . . . . . . . . . . . . . . 1-36
Array Plot Displays on App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-38
Array Plot Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-39
iii
Array Plot Configuration Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-40
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-41
iv Contents
1
When you complete the additional setup tasks, you can run Simulink models on Android devices.
1 On the MATLAB® Home tab, in the Environment section, select Add-Ons > Get Hardware
Support Packages.
2 In the Add-On Explorer window, click the support package and then click Install.
On the MATLAB Home tab, in the Environment section, select Help > Check for Updates.
1 On the MATLAB Home tab, in the Environment section, click Add-Ons > Manage Add-Ons.
2 In the Add-On Manager window, find and click the support package, and then click Uninstall.
1-2
Install Support for Android Devices
Hardware Setup
Hardware boards and devices supported by MathWorks® require additional configuration and setup
steps to connect to MATLAB and Simulink. Each support package provides a hardware setup process
that guides you through registering, configuring, and connecting to your hardware board.
If the support package is already installed, you can start the hardware setup by opening the Add-On
Manager.
In the Add-On Manager, start the hardware setup process by clicking the Setup button, .
After starting, the Hardware Setup window provides instructions for configuring the support package
to work with your hardware.
Follow the instructions on each page of the Hardware Setup window. When the hardware setup
process completes, you can open the examples to get familiar with the product and its features.
See Also
Related Examples
• “Getting Started with Android™ Devices”
More About
• “Get and Manage Add-Ons”
• “Manage Add-Ons”
• “Android Developer Options” on page 1-4
• “Collect Hardware Specifications from Android Device” on page 1-6
1-3
1 Install and Use Android Devices
1 Open the Settings menu on the device. The Settings menu can be accessed from the settings
icon, , in the Android apps screen.
2 Select About device from the Settings menu. Depending on the version of Android supplied
with the device, About device can be interchanged for any of the following alternatives:
• About
• About tablet
• About phone
3 Find Build Number within the list of About device information. Click the Build Number item
seven times to enable developer options.
Note Some Android devices provide a Search option at the top of the Settings menu that you
can use to search directly for Build Number.
4 Your Android device briefly displays a confirmation message that your device has developer
options enabled.
When Developer options have been enabled, a new { } Developer options menu item is displayed in
the settings. You can optionally disable developer options on your phone again by setting the switch
at the top of the { } Developer options to off.
1 Open the Settings menu on the device. The Settings menu can be accessed from the settings
icon, , in the Android apps screen.
2 Select System. This step is applicable only on Android 8.0 and higher.
3 Select {} Developer options from the menu.
4 Find USB debugging within the list of {} Developer options.
5 Click USB debugging to enable USB debugging on your Android device.
1-4
Android Developer Options
See Also
More About
• “Install Support for Android Devices” on page 1-2
• “Collect Hardware Specifications from Android Device” on page 1-6
External Websites
• https://developer.android.com/studio/debug/dev-options.html
1-5
1 Install and Use Android Devices
To get the hardware specifications from your Android device, the hardware setup installs and runs a
test app on your Android device. The test app creates an XML file containing your device hardware
specifications. To view the location of this file, enter the following code in the MATLAB command
window.
fullfile(prefdir,'MWAndroidDeviceInfo')
The hardware specification file is named after your device serial number, for example, 233507b.xml.
Warning Do not modify or delete this file. Some features of the support package are device-specific
and require this hardware specification file.
A separate XML gets generated for each device configured using the hardware setup process.
See Also
More About
• “Hardware Setup” on page 1-3
• “Android Developer Options” on page 1-4
1-6
Run Model on Android Devices
Tip This step overwrites the Model Configuration Parameters. To preserve the configuration of
an existing model, select File > Save As to create a working copy of the model that you can
configure.
Note If you do not see your Android device type in the Hardware board parameter list, follow
these instructions to “Install Support for Android Devices” on page 1-2.
1-7
1 Install and Use Android Devices
6 In the Hardware tab of the toolstrip, click Build, Deploy & Start.
This creates an app with the same name as the model. Simulink software then uses the USB
cable to load and run the model on the Android device.
When you redeploy the model to the Android device, it stops and replaces the previous instance of the
model.
See Also
“Communicating with Android™ Devices” | “Tune Parameters and Monitor Data in Model Running on
Android Devices” on page 1-9
1-8
Tune Parameters and Monitor Data in Model Running on Android Devices
• Find the optimal values for performance. This process is called parameter tuning.
• Accelerate parameter tuning because you do not have to rerun the model each time you change
parameters.
• Develop and validate your model using the actual data and hardware for which it is designed.
1 In the model on your host computer, click Monitor & Tune from Hardware tab .
2 (Optional) Place sink blocks in your model and connect them to signals.
3 Run the model on the Android device in Signal Monitoring and Parameter Tuning feature.
4 (Optional) Observe signals from Android device using Display or Scope blocks on your host
computer.
5 Change and apply parameter values in the model on your host computer.
6 Find the optimal parameter values by adjusting and observing the results.
7 Save the new parameter values, stop Signal Monitoring and Parameter Tuning feature, and save
the model.
The default value is 10.0 seconds. To run the model for an indefinite period, enter inf.
4 (Optional) To log data from the model while it is running on the Android device:
1-9
1 Install and Use Android Devices
Unlocking the screen enables the device to send sensor data back to the host computer.
Otherwise, with the screen locked, the host computer receives zeros instead of real sensor data.
7 To start the simulink, in the Simulation tab, click the Run button.
If your model does not contain a sink block, the Message Viewer displays a warning that “No
data has been selected for uploading.” You can disregard this warning or add a sink block to the
model.
After several minutes, Simulink starts running your model on the device.
External mode uses the Wi-Fi connection to exchange data with the host computer.
8 In the model on your host computer, change parameter values.
Any Simulink sink blocks in your model receive data from the hardware and display it on your
host computer.
Note External mode increases the processing burden of the model running on the device. If the
software reports an overrun, disable External mode.
If the Stop time parameter is set to a specific number of seconds, signal monitoring and parameter
stops when that time elapses.
See Also
Related Examples
• “Communicating with Android™ Devices”
1-10
Troubleshoot Task Overruns on Android Devices
Standard scheduling works well when a processor is moderately loaded. When a processor becomes
overloaded, the scheduler may run a task before the previous instance of the same task has
completed. The result is a task overrun.
3 Click OK.
When a task overrun occurs, the display on the device shows an “Overrun” error message until you
stop the model.
Note External mode increases the processing burden of the model running on the Android device. If
the software reports an overrun, disable External mode.
1-11
1 Install and Use Android Devices
3 In the Hardware tab of the toolstrip, click Build, Deploy & Start to build and deploy the
Simulink model to your Android device. For additional instructions on how to configure a model
for deployment to an Android device, see Getting Started with Android Devices.
4 Android Studio IDE requires the same tools that Simulink used to build the project. Execute the
function getAndroidSLSupportPkgToolPaths at the MATLAB command line to get the tool
paths.
getAndroidSLSupportPkgToolPaths
ans =
AndroidSDKLocation: 'C:\Users\AppData\Local\Android\Sdk'
JDKLocation: 'C:\Program Files\Android\Android Studio1\jre'
AndroidNDKLocation: 'C:\Users\AppData\Local\Android\Sdk\ndk\22.0.7026061'
1-12
Import Project to Google Android Studio
GradleHome: []
OpenCVLocation: 'D:\share\apps\RTT\Android\v005\opencv-android-sdk'
5 Open Android Studio and select Import project (Eclipse ADT, Gradle, etc).
6 Select the second-level project folder with the same name as the project. For example, in the
androidGettingStatedExample_ert_rtw project, select the
androidGettingStartedExample_ert_rtw\androidGettingStartedExample folder.
1-13
1 Install and Use Android Devices
7 Click OK to open and import your project to the Android Studio IDE.
8 In the Android Studio IDE window, select File > Project Structure.
1-14
Import Project to Google Android Studio
9 In the Project Structure window, select SDK Location from the left pane. The Android SDK
location, Android NDK location, and JDK location paths are automatically populated based
on the Android project. Verify whether these location values match the values returned on
executing the getAndroidSLSupportPkgToolPaths function. Click OK after you verify these
paths.
1-15
1 Install and Use Android Devices
10 To deploy the app on your Android device that uses the native code, from the project name drop-
down list below the Android Studio IDE toolbar, select Edit Configurations.
1-16
Import Project to Google Android Studio
13 From the device name drop-down list below the Android Studio IDE toolbar, select your device.
14 Click Run to run and deploy the application on your Android device.
1-17
1 Install and Use Android Devices
1-18
Install Android SDK Platform Packages and Tools
• From your Android Studio application toolbar, select Tools > Android > SDK Manager.
1-19
1 Install and Use Android Devices
3 In the Default Settings dialog box, click these tabs to install Android SDK platform packages and
developer tools.
Note The location of the Android SDK displayed in the Android SDK Location parameter must
not contain any white spaces. The spaces may cause problems when working with Android NDK
tools. You can change the SDK location by clicking the Edit button.
1-20
Install Android SDK Platform Packages and Tools
4 Click Apply. Android Studio starts installing the selected packages and tools on your computer.
After the installation is completed, the Status of the installed packages and tools changes from
Not installed to Installed.
5 Click OK.
See Also
“Android Developer Options” on page 1-4
1-21
1 Install and Use Android Devices
• On the Android Studio landing page, select Configure > Check for Update.
• From your Android Studio application toolbar, select Help > Check for Updates.
1-22
Update to the Latest Version of Android Studio
If a new version of Android Studio is available for download, a notification dialog box is
displayed.
3 In the notification dialog box, click Update and Restart.
See Also
“Android Developer Options” on page 1-4
1-23
1 Install and Use Android Devices
cd /Applications/Android Studio.app/Contents/gradle
3 Type chmod -R +x *. This command adds execute permission to all files and folders inside the
gradle folder.
1 Open Windows Explorer and right-click the gradle folder, which is located inside the Android
Studio installation folder on your computer.
2 Select Properties. In the gradle Properties window, click the Security tab.
3 To change the permissions, click Edit.
4 From the Group or user names box, select your group or user name.
5 From the Permissions for <User or Group> box, select the Allow option for Read & execute
permission.
6 Click Apply. Click OK to close the dialog box.
See Also
“Android Developer Options” on page 1-4
1-24
Publish or Retrieve Data to Internet of Things Using ThingSpeak
You can publish or retrieve data on your hardware board, such as sensor data, to the Internet of
Things using ThingSpeak Write or from the Internet of Things using ThingSpeak Read block.
For more information about how to use ThingSpeak, visit their Apps, Plugins, or Tutorials topics.
1-25
1 Install and Use Android Devices
1-26
Publish or Retrieve Data to Internet of Things Using ThingSpeak
2 In the ThingSpeak Write or ThingSpeak Read block, paste the key into the Write API key/Read
API Key parameter.
3 Save your changes to the model.
4 In the Hardware tab, click Build, Deploy & Start button.
After several minutes, the model application starts running on the target hardware.
5 On ThingSpeak.com, click the Private View tab or, if enabled, the Public View tab.
1-27
1 Install and Use Android Devices
The view displays the data that your target hardware is publishing to ThingSpeak.
See Also
ThingSpeak Read | ThingSpeak Write
1-28
Simulink Scope Block and Mobile Apps
• Display the scope on the device screen when you build, load, and run the model on your mobile
device.
• Show a real-time display of raw or processed sensor data on your device screen.
• Display multiple signals from various sources on a single scope display.
• Include multiple scope displays to group related data signals.
• Match your preferences by customizing the appearance of the scope.
5 In the Sine Wave block, set the Sine type to Sample based and Sample time to 1. Click OK.
6 In the model, click Run.
1-29
1 Install and Use Android Devices
7 To open the Scope viewer and see the sine signal, double-click the Scope block.
8 In the Modeling tab of the toolstrip, select Model Settings to open the Configuration
Parameters dialog box.
9 Select the Hardware Implementation pane and from the Hardware board parameter list,
select Android Device. Click OK.
10 In the model, click Deploy to Hardware to build the app onto your Android device.
1-30
Simulink Scope Block and Mobile Apps
When the app starts running on your device, you can see the equivalent scope display on your device
screen.
In a model with multiple Scope blocks, the scope displays in your app are shown on multiple pages.
Below the scope displays, a page indicator widget is automatically generated to show the current
scope selection. Click to the right or left of the page indicator widget to cycle between the available
scope displays.
Note If a model contains a Camera or Video Display block and a Scope block, then the camera or
video display is first page, followed by the other scope displays.
1-31
1 Install and Use Android Devices
On the device screen, each scope display provides a default and a full screen viewing mode. The
default view splits the app display into two parts. The left half shows the scope displays and the right
half shows the UI widgets, including the Button, Slider, and Data Display blocks, in your model. The
full screen view expands the scope display to fill the device screen, allowing a more detailed
inspection of signals. To toggle between the two views, click the arrow button in the top right corner
of the scope display on your device screen.
On the device screen, you can adjust a scope display using typical interactive gestures. By tapping
and dragging your finger across the screen, you can move the display axes horizontally and vertically.
By tapping two fingers on the screen and moving them closer or farther apart, you can zoom in and
out of the axes.
Scope Style
To modify the appearance of the scope in your app, you can use a subset of the style properties
available to the scope on your development computer.
In the scope display, open the Style dialog box by selecting View > Style.
For more information on general configuration properties, see the Scope block.
1-32
Simulink Scope Block and Mobile Apps
Axes colors
Select the background color for axes (displays) with the first color palette. Select the ticks, labels,
and grid color with the second color palette.
Line
Select the line style, width, and color. The scope displayed on the app supports continuous,
dotted, and dashed line styles only. All other line style selections automatically default to
continuous.
Note All other scope style parameters use their default value for the scope display in the app.
To open the Configuration Properties dialog box, in the scope display box, click View >
Configuration properties.
For more information on general configuration properties and their default settings, see the Scope
block.
1-33
1 Install and Use Android Devices
From the legend, you can control which signals are visible. This control is equivalent to changing
the visibility in the Style properties. In the scope legend, click a signal name to hide the signal in
the scope. To show the signal, click the signal name again. To show only one signal, right-click the
signal name, which hides all other signals.
Axes scaling
Specify when to scale the y-axis to display all signal values. The axis scaling of the scope
displayed on the app supports Manual and Auto configurations only.
Y-limits (Minimum)
Specify the minimum value of the y-axis, as a real number.
Y-limits (Maximum)
Specify the maximum value of the y-axis, as a real number.
1-34
Simulink Scope Block and Mobile Apps
Y-label
Specify the text to display on the y-axis.
Note All other scope configuration parameters use their default values for the scope in the
generated app.
Limitations
The following limitations apply. These limitations also describe the differences between the scope
behavior while the model is running and when the model builds to the hardware.
• When a Scope block in a model runs on the development computer, the horizontal scope axis
displays the model time step. However, the horizontal scope axis on the app displays the integer
sample number of the signal.
• If a previous version of the app project exists in the current folder, style properties of the scope
are not automatically applied in the app. For scope style properties to take effect in the app, you
must remove the previous project by deleting this folder: <your model name>_ert_rtw.
• After you click Deploy to Hardware to start generating the app, changes made to the
configuration properties of the scope are not automatically applied. For the scope configuration
properties to take effect in the app, you must remove the previous generated project. To remove
this project, delete the folder <your model name>_ert_rtw in your working folder.
• Layouts or multiple axes within one scope display are not supported for scope displays in a
generated app.
• Scopes are displayed on the app in landscape view only.
See Also
Related Examples
• “Scope Block with Android Devices”
1-35
1 Install and Use Android Devices
• Display the array plot on the device screen when you build, load, and run the model on your
mobile device.
• Show a real-time display of raw or processed sensor data on your device screen.
• Display multiple signals from various sources on a single array plot display.
• Include multiple array plot displays to group related data signals.
• Match your preferences by customizing the appearance of the array plot.
1-36
DSP System Toolbox Array Plot Block and Mobile Apps
11 To open the Array Plot viewer and see the histogram of the signal, double-click the Array Plot
block. The histogram takes the form of a normal distribution curve.
12 In the model, click Deploy to Hardware to build the app onto your Android device.
1-37
1 Install and Use Android Devices
When the app starts running on your device, you can see the equivalent array plot display on your
device screen.
In a model with multiple Array Plot or Scope blocks, the array plot and scope displays in your app are
shown on multiple pages.
Below the array plot displays, a page indicator widget is automatically generated to show the current
array plot selection. Click to the right or left of the page indicator widget to cycle between the
available array plot displays.
1-38
DSP System Toolbox Array Plot Block and Mobile Apps
Note If a model contains a Camera, Video Display block, Scope block, and Array Plot block, then the
camera or video display is first page, followed by the other scope or array plot displays.
On the device screen, each array plot display provides a default and a full screen viewing mode. The
default view splits the app display into two parts. The left half shows the array plot displays and the
right half shows the UI widgets, including the Button, Slider, and Data Display blocks, in your model.
The full screen view expands the array plot display to fill the device screen, allowing a more detailed
inspection of signals. To toggle between the two views, click the arrow button in the top right corner
of the array plot display on your device screen.
On the device screen, you can adjust an array plot display using typical interactive gestures. By
tapping and dragging your finger across the screen, you can move the display axes horizontally and
vertically. By tapping two fingers on the screen and moving them closer or farther apart, you can
zoom in and out of the axes.
To open the Style dialog box from the array plot display box, click View > Style.
For more information on general configuration properties, see the Array Plot block.
1-39
1 Install and Use Android Devices
Plot type
The styles of plot used to display the signal:
• Line
• Stairs
• Stem
Axes colors
Select the background color for axes (displays) with the first color palette. Select the ticks, labels,
and grid color with the second color palette.
Line
Select the line style, width, and color. The array plot displayed on the app supports continuous,
dotted, and dashed line styles only. All other line style selections automatically default to
continuous.
Note All other array plot style parameters use their default value for the array plot display in the
app.
To open the Configuration Properties dialog box, in the scope display box, click View >
Configuration properties.
For more information on general configuration properties and their default settings, see the Array
Plot block.
1-40
DSP System Toolbox Array Plot Block and Mobile Apps
Sample increment
Specify the spacing between samples along the x-axis as a finite numeric scalar. The input signal
is only y-axis data. X-axis data is set automatically based on both the Sample increment and X-
offset values. For example, when X-offset is 0 and Sample increment is 1, the x-data for the input
signal is set to 0, 1, 2, 3, 4, … . If you set Sample increment to 0.25, the x-axis data becomes 0,
0.25, 0.5, 0.75, 1, … .
X-offset
Specify the offset to apply to the x-axis, as a numeric scalar. X-axis data is set automatically based
on both the Sample increment and X-offset values. The offset represents the first value on the x-
axis. For example, when X-offset is 0 and Sample increment is 1, the x-data for the input signal is
set to 0, 1, 2, 3, 4, … . If you set X-offset to -3, the x-axis data becomes -3, -2, -1, 0, 1, … .
Show legend
Show signal legend. The names listed in the legend are the signal names from the model. For
signals with multiple channels, a channel index is appended after the signal name.
Show grid
Show vertical and horizontal grid lines.
Y-limits (Minimum)
Specify the minimum value of the y-axis, as a real number.
Y-limits (Maximum)
Specify the maximum value of the y-axis, as a real number.
Note All other array plot configuration parameters use their default values for the array plot in the
generated app.
Limitations
The following limitations apply. These limitations also describe the differences between the array plot
behavior while the model is running and when the model builds to the hardware.
• After you click Deploy to Hardware to start generating the app, changes made to the
configuration properties of the scope are not automatically applied. For the array plot
1-41
1 Install and Use Android Devices
configuration properties to take effect in the app, you must remove the previous generated
project. To remove this project, delete the folder <your model name>_ert_rtw in your working
folder.
• Layouts or multiple axes within one array plot display are not supported for array plot displays in
a generated app.
• Array plots are displayed on the app in landscape view only.
1-42
2
Connected I/O
2 Connected I/O
Normal mode simulation with Connected I/O is an intermediate step in the Model-Based Design
workflow that bridges the gap between simulation and code generation by enabling Simulink to
communicate with the hardware before deploying the model on hardware. Connected I/O enables you
to modify your model design and monitor the effect of the modified design using peripheral data from
the hardware in a near real-time environment. You are not required to deploy the model on the
hardware to monitor the effect of the modified design, which accelerates the simulation process. This
interaction between Simulink (software) and Android device (hardware) is possible in Normal mode
simulation only when you enable Connected I/O.
In this section...
“Android Blocks Supporting Connected I/O” on page 2-2
“How Connected I/O Works” on page 2-2
“Connected I/O in Model-Based Design” on page 2-3
“How Connected I/O Differs from External Mode” on page 2-4
“How to Enable Connected I/O” on page 2-5
• Accelerometer
• Gyroscope
• Light Sensor
• Ambient Temperature Sensor
• Pressure Sensor
• Humidity Sensor
• Location Sensor
• Orientation
• Magnetometer
2-2
Communicate with Hardware in Normal Mode Simulation Using Connected I/O
This diagram shows the connection that the Connected I/O creates between your computer and the
hardware.
When you simulate a Simulink model in Normal mode with Connected I/O:
1 The device driver block (for example, Accelerometer block) in the model request sensor data
from the IO Server.
2 The IO Server accepts the request and responds with the requested data. You can use any
Simulink sink or dashboard block to view the received data. Using the peripheral data received,
you can verify that your model design meets the requirements.
3 If necessary, you can modify the design by adding, removing, or replacing any block in the
Simulink model.
4 After the model is modified, resimulate the model. During simulation, the data request from the
model is communicated to the hardware. You can continue to modify and simulate the model until
the expected behavior is achieved.
Note
• The communication in Connected I/O is an on-demand process. The hardware sends data only
when receiving a data request from the Simulink model.
• You do not have to build, deploy, and run the model on the hardware to monitor the effects of your
changes in your model design.
2-3
2 Connected I/O
simulation and code generation by enabling Simulink to communicate with the hardware before
deploying the model on the hardware.
a Normal mode simulation without Connected I/O: There is no hardware interaction and no
code generation.
b Normal mode simulation with Connected I/O: The model communicates with the hardware.
There is no code generation.
c External mode: The model is deployed on the hardware and generates code.
3 Deploy the model to the hardware.
2-4
Communicate with Hardware in Normal Mode Simulation Using Connected I/O
2-5
2 Connected I/O
• Network
• Name: Displays the name of the network to which the Android device is connected.
• IP Address: Displays the IP address of the Android device.
Note To establish a connection between the Android device and the host computer, ensure
that they are connected on the same wi-fi network.
2-6
Communicate with Hardware in Normal Mode Simulation Using Connected I/O
• Device
• Model
This section displays data only from sensors supported by the connected Android device. For
example, if the Android device does not support a humidity sensor, the section displays the
message No relative Humidity Support. Data from the supported sensors is displayed in
this section during simulation.
Note While using the Location Sensor into the Simulink model, you will be prompted to set the
location permission for obtaining the location information of the Android device. Once you grant
the permission, the location data of the device is displayed in Simulink when Connected I/O runs.
Otherwise, 0 is displayed. The input data obtained from other connected sensors is not affected.
10 To close the SimulinkIOServer application, disable Connected I/O.
2-7
3
• To Workspace
• Outport
Without MAT-file logging on the Android device, you can only log and analyze signals on the device
through External mode or by sending the signal to a computer (using UDP or TCP) and storing it in
MATLAB. These mechanisms need an active connection between the computer and the device when
logging data. With MAT-file logging, you can log signals without any need to connect the device to a
computer. Other advantages of logging signals to MAT-files over other mechanisms are:
Before you start to save signals from Simulink models on the Android device, complete the steps
listed in “Prerequisites for Logging Signals” on page 3-3.
1 Configure Board Parameters and Enable MAT-File Logging on page 3-4: To save MAT-files on
the Android device, the MAT-file logging option in the Configuration Parameters dialog box
must be selected. Also, the device parameters must be specified.
2 “Configure Model to Log Signals on the Android Device” on page 3-6: MAT-file logging is
supported in models containing To Workspace or Outport blocks. Specify the values for several
block parameters.
3 Run Model on the Android Device on page 3-11: Simulink deploys code and logs signals on the
Android device as MAT-files.
4 Import MAT-Files into MATLAB on page 3-12: After logging is complete, you can open MAT-files
in MATLAB and use them for further analysis.
See Also
Related Examples
• “Plot Route and Speed on a Map Using MAT-File Logging on an Android Device”
3-2
Prerequisites for Logging Signals
Outport block
3-3
3 MAT-File Logging on the Hardware
1 In your model window, open the Configuration Parameters dialog box, go to the Hardware
Implementation pane, and select the name of the hardware from the Hardware board list.
2 In the Hardware board settings pane, expand Target hardware resources and select Device
options.
3 From the Device list, select your Android device. If your device is not listed, click Refresh.
Note If your device is not listed even after clicking the Refresh button, ensure that you have
enabled the USB debugging option on your device. To enable USB debugging, enter
androidhwsetup in the MATLAB Command Window and follow the onscreen instructions.
4 In the Configuration Parameters dialog box, go to Code Generation > Interface and expand
Advanced parameters, or type MAT-file logging in the search box.
5 Select the MAT-file logging option.
6 From the MAT-file variable name modifier list, select an option to add rt_ as prefix or _rt as
suffix to the variable name in the MAT-file. To use a variable name without any modification,
select none.
7 Click Apply to save your changes, and then click OK.
3-4
Configure Board Parameters and Enable MAT-File Logging
3-5
3 MAT-File Logging on the Hardware
1 On the Simulink model toolbar, set the Simulation stop time, located to the left of Simulation
mode. The Simulation stop time parameter specifies the duration for which the signals are logged
in the MAT-file. After the Simulation stop time elapses, the logging of the signal stops. However,
the model continues to run on the device. For example, if the Simulation stop time parameter is
specified as 10.0 seconds, the logging stops after 10.0 seconds. However, the model continues to
run for an indefinite time. If the Simulation stop time parameter is specified as Inf, the logging
continues until the device memory is full, or you close the application.
2 In the Simulink model, set the parameter values of To Workspace and Outport blocks.
To Workspace Block
To set the parameter values of the To Workspace block:
1 Double-click the block, and specify these parameters in the Block Parameter dialog box.
Parameter Description
Variable name Specify a variable name for the signal to be
logged.
Limit data points to last Specify the number of data points to be
logged in the MAT-file. The maximum number
of data points that a MAT-file can contain is
512.
Decimation Use this parameter to write data points at
every nth sample, where n is the decimation
factor. The default decimation, 1, writes data
at every time sample.
3-6
Configure Model to Log Signals on the Android Device
Parameter Description
Save format Select a format of the variable to which you
save data. MAT-file logging supports only
these three formats: Array, Structure
with Time, or Structure.
Outport Block
To set the parameter values of the Outport block:
3-7
3 MAT-File Logging on the Hardware
1 Double-click the block, select the Signal Attributes tab, and specify the Sample time
parameter. When you specify this parameter as –1, the sample time is inherited from the driving
block.
2 In the model window, open the Configuration Parameters dialog box and select Data Import/
Export.
3-8
Configure Model to Log Signals on the Android Device
Parameter Description
Time Saves time data to the specified variable.
Output Saves signal data to the specified variable.
Format Select a format of the variable to which you
save data. MAT-file logging supports only
these three formats: Array, Structure
with Time, or Structure.
3-9
3 MAT-File Logging on the Hardware
Parameter Description
Limit data points to last Specify the number of data points to be
logged in a MAT-file. The maximum number
of data points that a MAT-file can contain is
512.
Decimation Use this parameter to write data points at
every nth sample, where n is the decimation
factor. The default decimation, 1, writes data
at every time sample.
3-10
Run Model on the Android Device
The application starts running and logs signals in MAT-files on the device. The files are named as
<modelname>_<runnumber>_<indexnumber>.mat. The name of your Simulink model is
modelname. runnumber is the number of times the model is run. runnumber starts with 1 and is
incremented by one for every successive run. indexnumber is the MAT-file number in a run.
indexnumber starts with 1 and is incremented by one for every new file that is created in the same
run.
Suppose that the name of the model is androidMatFile. In the first run, Simulink creates
androidMatFile_1_1.mat file and starts logging data in this file. After the logging in the first file is
completed, Simulink creates androidMatFile_1_2.mat file and continues logging data in this file.
Likewise, the logging continues in multiple MAT-files until the Simulation stop time is elapsed. If
the same model is run again, the new files are named as androidMatFile_2_1.mat,
androidMatfile_2_2.mat, and so on.
After the Simulation stop time elapses, the logging of signal stops.
• The total file size exceeds the available device storage capacity.
• The signal logging rate is faster than the device writing speed.
3-11
3 MAT-File Logging on the Hardware
variable1 is a MATLAB variable that displays the status of copy operation. variable2 displays
the list of the MAT-files copied to MATLAB. In an unsuccessful copy operation, variable2
displays the error message.
For example,
result =
matFiles =
After successfully copying the files, the value of the result variable becomes 0. The matFiles
variable displays the names of the MAT-files copied to MATLAB. You can also see the copied files
in the MATLAB Current Folder window.
If MATLAB encounters any issue when copying the files, the value of the result variable
changes to a nonzero value. The matFiles variable displays the corresponding error message.
2 Load the variables in the MAT-file into MATLAB workspace variables.
load('modelname*.mat');
For example,
load('androidMatFile*.mat');
3-12
Import MAT-Files into MATLAB
After copying the files to MATLAB, you can use them like conventional MAT-files. For example,
you can load and plot data points using the load and plot commands in the MATLAB command
window.
See Also
Related Examples
• “Plot Route and Speed on a Map Using MAT-File Logging on an Android Device”
3-13