0% found this document useful (0 votes)
12 views68 pages

Android Ug

The document is a user's guide for the Simulink Support Package for Android Devices, detailing installation, usage, and troubleshooting procedures. It includes sections on setting up hardware, enabling developer options, running models, and logging signals on Android devices. The guide also provides contact information for MathWorks and outlines the software's licensing and trademark information.

Uploaded by

Minh Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views68 pages

Android Ug

The document is a user's guide for the Simulink Support Package for Android Devices, detailing installation, usage, and troubleshooting procedures. It includes sections on setting up hardware, enabling developer options, running models, and logging signals on Android devices. The guide also provides contact information for MathWorks and outlines the software's licensing and trademark information.

Uploaded by

Minh Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Simulink® Support Package for Android™

Devices
User’s Guide

R2021a
How to Contact MathWorks

Latest news: www.mathworks.com

Sales and services: www.mathworks.com/sales_and_services

User community: www.mathworks.com/matlabcentral

Technical support: www.mathworks.com/support/contact_us

Phone: 508-647-7000

The MathWorks, Inc.


1 Apple Hill Drive
Natick, MA 01760-2098
Simulink® Support Package for Android™ Devices User's Guide
© COPYRIGHT 2014–2021 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used or copied
only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form
without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through
the federal government of the United States. By accepting delivery of the Program or Documentation, the government
hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer
software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014.
Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain
to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and
Documentation by the federal government (or other entity acquiring for or through the federal government) and shall
supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is
inconsistent in any respect with federal procurement law, the government agrees to return the Program and
Documentation, unused, to The MathWorks, Inc.
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be
trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for
more information.
Revision History
October 2014 Online only Revised for Version 14.2.0 (R2014b)
March 2015 Online only Revised for Version 15.1.0 (R2015a)
September 2015 Online only Revised for Version 15.2.0 (R2015b)
March 2016 Online only Revised for Version 16.1.0 (R2016a)
September 2016 Online only Revised for Version 16.2.0 (R2016b)
March 2017 Online only Revised for Version 17.1.0 (R2017a)
September 2017 Online only Revised for Version 17.2.0 (R2017b)
August 2018 Online only Revised for Version 17.2.1 (R2017b)
March 2018 Online only Revised for Version 18.1.0 (R2018a)
August 2018 Online only Revised for Version 18.1.3 (R2018a)
September 2018 Online only Revised for Version 18.2.0 (R2018b)
March 2019 Online only Revised for Version 19.1.0 (R2019a)
September 2019 Online only Revised for Version 19.2.0 (R2019b)
March 2020 Online only Revised for Version 20.1.0 (R2020a)
September 2020 Online only Revised for Version 20.2.0 (R2020b)
March 2021 Online only Revised for Version 21.1.0 (R2021a)
April 2021 Online only Revised for Version 21.1.1 (R2021a)
May 2021 Online only Revised for Version 21.1.2 (R2021a)
July 2021 Online only Revised for Version 21.1.3 (R2021a)
Contents

Install and Use Android Devices


1
Install Support for Android Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Install, Update, or Uninstall Support Package . . . . . . . . . . . . . . . . . . . . . . 1-2
Hardware Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3

Android Developer Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4


Enable On-device Developer Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
Enable USB Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4

Collect Hardware Specifications from Android Device . . . . . . . . . . . . . . . . 1-6

Run Model on Android Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7

Tune Parameters and Monitor Data in Model Running on Android Devices


.......................................................... 1-9
Signal Monitoring and Parameter Tuning . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Monitor and Tune Your Simulink Model . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Stop Monitor and Tune . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10

Troubleshoot Task Overruns on Android Devices . . . . . . . . . . . . . . . . . . . 1-11

Import Project to Google Android Studio . . . . . . . . . . . . . . . . . . . . . . . . . 1-12

Install Android SDK Platform Packages and Tools . . . . . . . . . . . . . . . . . . 1-19

Update to the Latest Version of Android Studio . . . . . . . . . . . . . . . . . . . . 1-22

Grant Execute Permission on Gradle Folder . . . . . . . . . . . . . . . . . . . . . . . 1-24

Publish or Retrieve Data to Internet of Things Using ThingSpeak . . . . . 1-25


Set Up the Model and ThingSpeak . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-25
Update and Deploy the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-27

Simulink Scope Block and Mobile Apps . . . . . . . . . . . . . . . . . . . . . . . . . . 1-29


Add Simulink Scope to App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-29
Scope Displays on App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-31
Scope Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-32
Scope Configuration Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-33
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-35

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

MAT-File Logging on the Hardware


3
Log Signals in MAT-Files on an Android Device . . . . . . . . . . . . . . . . . . . . . 3-2

Prerequisites for Logging Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3

Configure Board Parameters and Enable MAT-File Logging . . . . . . . . . . . 3-4

Configure Model to Log Signals on the Android Device . . . . . . . . . . . . . . . 3-6


To Workspace Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
Outport Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7

Run Model on the Android Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11

Import MAT-Files into MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12

iv Contents
1

Install and Use Android Devices

• “Install Support for Android Devices” on page 1-2


• “Android Developer Options” on page 1-4
• “Collect Hardware Specifications from Android Device” on page 1-6
• “Run Model on Android Devices” on page 1-7
• “Tune Parameters and Monitor Data in Model Running on Android Devices” on page 1-9
• “Troubleshoot Task Overruns on Android Devices” on page 1-11
• “Import Project to Google Android Studio” on page 1-12
• “Install Android SDK Platform Packages and Tools” on page 1-19
• “Update to the Latest Version of Android Studio” on page 1-22
• “Grant Execute Permission on Gradle Folder” on page 1-24
• “Publish or Retrieve Data to Internet of Things Using ThingSpeak” on page 1-25
• “Simulink Scope Block and Mobile Apps” on page 1-29
• “DSP System Toolbox Array Plot Block and Mobile Apps” on page 1-36
1 Install and Use Android Devices

Install Support for Android Devices


Installing the Simulink Support Package for Android Devices adds the following items on your host
computer:

• Third-party software development tools


• A block library called Simulink Support Package for Android Devices.
• Examples

When you complete the additional setup tasks, you can run Simulink models on Android devices.

Install, Update, or Uninstall Support Package


Install Support Package

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.

Update Support Package

On the MATLAB Home tab, in the Environment section, select Help > Check for Updates.

Uninstall Support Package

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

Android Developer Options


Loading an application onto your Android device from your development computer requires that the
developer options be enabled on your device. Additionally, your development computer uses the
Android Debug Bridge (ADB) to load and run apps on your Android device. The ADB communicates
using the USB cable which requires USB debugging enabled on your Android device.

Enable On-device Developer Options


By default, most Android devices are sold with the developer options disabled. To enable developer
options on an Android device, use the following steps:

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.

Enable USB Debugging


To enable USB debugging on an Android device, use the following steps:

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

Collect Hardware Specifications from Android Device


Many of the blocks provided in the Simulink Support Package for Android Devices requires the
hardware specifications of your Android device, such as the available screen resolutions for the
Camera block.

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

Run Model on Android Devices


This example shows how to configure and run a model on Android device.
1 Connect the Android device to your host computer using a USB cable.
2 Connect the Android device to the same WiFi network used by your host computer.
3 Create a new Simulink model or open an existing model.
4 In the Modeling tab of the toolstrip, select Model Settings.
5 In the Configuration Parameter dialog box, click Hardware Implementation. Set Hardware
board to Android Device. Then, click OK.

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.

To stop a model running on your Android device:

• Close the app containing the model on your Android device.


• If you are using external mode, click the Stop button on the Hardware tab of the toolstrip.

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

Tune Parameters and Monitor Data in Model Running on


Android Devices
In this section...
“Signal Monitoring and Parameter Tuning” on page 1-9
“Monitor and Tune Your Simulink Model” on page 1-9
“Stop Monitor and Tune” on page 1-10

Signal Monitoring and Parameter Tuning


You can use Monitor and Tune feature (also called External mode) to tune parameters of a model
running on your Android device and monitor the effects of different parameter values in real time.
Monitor and Tune feature helps you:

• 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.

This workflow lists the tasks required to tune parameters:

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.

Monitor and Tune Your Simulink Model


1 Verify that you can run your Simulink model on the Android device, as described in “Run Model
on Android Devices” on page 1-7.
2 Verify that the Android device is connected to a Wi-Fi network, and to the USB port on the Host
computer.
3 Set the Stop time parameter, located in the Simulation tab of toolstrip.

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

• Right-click the signal line and select Log Selected Signals.


• Connect a To File or To Workspace block to a signal in the model.
5 (Optional) To display data from the model while it is running on the Android device, connect a
Display, Floating Scope, or Scope block to a signal in the model.
6 If it is locked, unlock the screen on the Android device.

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.

Observe the corresponding changes in the model running on the hardware.

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.

Stop Monitor and Tune


To stop signal monitoring and parameter tuning, click the Stop button located in the Hardware tab
of the toolstrip.

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

Troubleshoot Task Overruns on Android Devices


You can configure a model running on the Android device to notify you when a task overrun occurs.

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.

To enable overrun detection:

1 In the Modeling tab of the toolstrip, select Model Settings.


2 On the Hardware Implementation pane, select the Enable overrun detection check box.

3 Click OK.

When a task overrun occurs, the display on the device shows an “Overrun” error message until you
stop the model.

To fix an overrun condition:

• Simplify the model.


• Increase the sample times for the model and the blocks in it. For example, change the Sample
time parameter in all of your data sources or sensor blocks, from 0.1 to 0.2.

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

Import Project to Google Android Studio


This example shows you how to import and build a project generated from a Simulink model to the
Google Android Studio IDE.
1 Download and install the Google Android Studio IDE version or you can use the Android Studio
IDE version installed on your Android device. For more information on how to install Android
SDK, see “Install Android SDK Platform Packages and Tools” on page 1-19.
2 Open the Simulink model Getting Started with Android Devices.
androidGettingStartedExample

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

11 In the Deploy drop-down list, select APK from app bundle.


12 Click Apply > OK.

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

Install Android SDK Platform Packages and Tools


To run Simulink models on your Android device, you must install Android Software Development Kit
(SDK) platform packages and tools on your computer. You can install Android SDK platform packages
and tools from Android Studio.

1 Start Android Studio.


2 To open SDK Manager, do any of the these:

• On Android Studio landing page, select Configure > SDK Manager.

• From your Android Studio application toolbar, select Tools > Android > SDK Manager.

1-19
1 Install and Use Android Devices

• From your Android Studio application toolbar, click SDK Manager.

3 In the Default Settings dialog box, click these tabs to install Android SDK platform packages and
developer tools.

a SDK Platforms: Select the latest Android SDK package.


b SDK Tools: Select these Android SDK tools:

• Android SDK Build-Tools


• NDK (Side by side)
• Android SDK Platform-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

Update to the Latest Version of Android Studio


To run Simulink models on your Android device, you must update Android Studio to the latest version
available for download.

1 Start Android Studio.


2 To check for the latest update available for download, do any of these:

• 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

Grant Execute Permission on Gradle Folder


To run Simulink models on your Android device, you must grant execute permission on the gradle
folder located inside the installation folder of Android Studio.

To grant execute permission on a Mac:

1 Open the Mac terminal window.


2 In the terminal, navigate to the gradle folder, which is located inside the Android Studio
installation folder on your computer. For example,

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.

To grant execute permission in Windows®:

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

Publish or Retrieve Data to Internet of Things Using


ThingSpeak
In this section...
“Set Up the Model and ThingSpeak” on page 1-25
“Update and Deploy the Model” on page 1-27

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.

Set Up the Model and ThingSpeak


1 Configure your Simulink model to run on the target hardware.
2 Add a ThingSpeak Write or ThingSpeak Read block to the model.
3 (Optional) Open the ThingSpeak Write or ThingSpeak Read block and configure its parameters.
4 Connect signals from your model to the block input on the ThingSpeak Write block or block
output on the ThingSpeak Read block.
5 Sign up for an account at https://thingspeak.com/ and log in.
6 Click Create New Channel.
7 Update the channel’s name and fields for the data you are publishing.

1-25
1 Install and Use Android Devices

1-26
Publish or Retrieve Data to Internet of Things Using ThingSpeak

8 Click Update Channel.

Update and Deploy the Model


1 Click the API Keys tab and copy the Write API Key/Read API Key.

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

Simulink Scope Block and Mobile Apps


Using the Simulink Scope block with the Simulink Support Package for Android Devices, you can:

• 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.

Add Simulink Scope to App


1 Create a Simulink model.
2 Open the Simulink Library Browser. From the Simulink > Sources library, add a Sine Wave
block to your model.
3 From the Simulink > Sinks library, add a Scope block to your model.
4 In the model, connect the Sine Wave block to the Scope block.

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.

Scope Displays on App


Multiple Scopes

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

Default and Full Screen Display

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.

Interaction with the Display

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.

Scope Configuration Properties


You can modify the configuration of the scope properties in your app by using a subset of the
configuration properties available to the scope on your development computer.

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

Number of input ports


Specify an integer number of input ports on a Scope block. The maximum number of input ports
is 96.
Input processing
Specify sample-based or frame-based processing of signals.
Time span
Specify the length of the x-axis range to display. The time span of the scope displayed on the app
automatically shows 10 times the value of the time span.
Show grid
Show vertical and horizontal grid lines.
Show legend
Toggle 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. Continuous
signals have straight lines before their names, and discrete signals have step-shaped lines.

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

DSP System Toolbox Array Plot Block and Mobile Apps


Using the DSP System Toolbox Array Plot block with the Simulink Support Package for Android
Devices, you can:

• 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.

Add DSP System Toolbox Array Plot to App


1 Create a Simulink model.
2 In the Modeling tab of the toolstrip, select Model Settings to open the Configuration
Parameters dialog box.
3 Select the Hardware Implementation pane and from the Hardware board parameter list,
select Android Device. Click OK.
4 Open the Simulink Library Browser. From the Simulink > Sources library, add a Random
Number block to your model.
5 From the DSP System Toolbox > Statistics library, add a Histogram block to your model.
6 From the DSP System Toolbox > Sinks library, add a Array Plot block to your model.
7 In the model, connect the three blocks as shown.

8 In the Histogram block, set the properties as follows.

• Set Lower limit of histogram to -5.0.


• Set Lower limit of histogram to 5.0.
• Set Number of bins ot 101.
• Set Find the histogram over to Entire Input.
• Check Normalized.
• Check Running histogram.
9 In the Random Number block, set the Sample time to 0.01. Click OK.
10 In the model, click Run.

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.

Array Plot Displays on App


Multiple Array Plots or Scopes

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.

Default and Full Screen Display

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.

Interaction with the Display

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.

Array Plot Style


To modify the appearance of the array plot in your app, you can use a subset of the style properties
available to the scope on your development computer.

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.

Array Plot Configuration Properties


You can modify the configuration of the array plot properties in your app by using a subset of the
configuration properties available to the array plot on your development computer.

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

Communicate with Hardware in Normal Mode Simulation Using


Connected I/O
You can use Connected I/O to communicate with the IO peripherals on the hardware during Normal
mode simulation.

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.

These sections explain:

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

Android Blocks Supporting Connected I/O


Use Connected I/O with the following sensor blocks from the Simulink Support Package for Android
Devices. Without Connected I/O, these sensor blocks output zero during Normal mode simulation.
With Connected I/O, these blocks read data from the connected sensors during Normal mode
simulation.

• Accelerometer
• Gyroscope
• Light Sensor
• Ambient Temperature Sensor
• Pressure Sensor
• Humidity Sensor
• Location Sensor
• Orientation
• Magnetometer

How Connected I/O Works


Connected I/O creates a communication interface that enables the Simulink model and the IO Server
to communicate with each other. The Simulink model resides in your computer, and the IO Server is
an engine on the hardware that contains all the peripheral functions. The transport layer formats and
transmits the data using the communication interface.

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.

Communication in Normal Mode Simulation with Connected I/O

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.

Connected I/O in Model-Based Design


When you simulate a model in Normal mode without Connected I/O, Simulink does not communicate
with the hardware. Simulink communicates with the hardware only when the code is generated and
the model is deployed on the hardware in External mode. Normal mode simulation with Connected
I/O is an intermediate step in the model-based design workflow that bridges the gap between

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.

This Model-Based Design Workflow diagram displays a model-based workflow:

1 Create a Simulink model.


2 Simulate the model in:

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.

Model-Based Design Workflow

How Connected I/O Differs from External Mode


Connected I/O and External mode both enable you to communicate with the hardware during
simulation. However, you use Connected I/O and External mode for different purposes. The table
shows the actions that you can perform with each mode.

Action External Mode Connected I/O


Obtain real-time data You can obtain real-time data Enable the Simulink Pacing
with External mode. Option to get near real-time
data.

2-4
Communicate with Hardware in Normal Mode Simulation Using Connected I/O

Action External Mode Connected I/O


Timing analysis of real-time Timing analysis of real-time data Timing analysis of real-time data
data is possible because the Simulink is not possible because the
model is running on the Simulink model is running in
hardware in real-time. your computer and not on the
hardware.
Time required to start 1-2 minutes Few seconds
simulation
Code generation Code is generated on the No code is generated.
hardware.

How to Enable Connected I/O


To simulate a model in Connected I/O during Normal mode simulation, you must enable the
Connected I/O option:

1 Create a Simulink model using the sensor and Display blocks.


2 In the Simulation tab, set the Simulation Mode to Normal.
3 In the Modeling tab, select Model Settings.
4 In the Configuration Parameter dialog box, select Hardware Implementation.
5 Set the Hardware board parameter to Android Device. This selection automatically populates
the Hardware board settings parameter with the default values for the Android hardware.
6 From the Groups list under Target hardware resources, select Connected I/O. The Enable
Connected I/O option is selected by default.

7 Click Apply. Click OK to close the dialog box.


8 Additionally, you can change the rate of simulation by enabling the Simulink Pacing Option as
described in “Simulation Pacing”.
9 Enter the Stop Time in the Simulation tab and click Run. The SimulinkIOServer Android
application is now installed on the Android device.

2-5
2 Connected I/O

The SimulinkIOServer application shows the following details:

• 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

• Serial: Displays the serial number of the Android 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

MAT-File Logging on the Hardware


3 MAT-File Logging on the Hardware

Log Signals in MAT-Files on an Android Device


You can use MAT-file logging to save signals from Simulink models on an Android device. The signals
from these models are saved as data points in MAT-files. With the data you log, you can apply fault
analysis, search for transient behavior, or analyze sensor data collected over a long period of time.
The data points can be saved in: Structure, Structure with time, or Array format. Simulink
supports logging signals on the Android device from only these two blocks:

• 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:

• The ability to collect signals over a long duration for analysis.


• The ability to store the signals in a well-structured MAT-file, including timestamp information.

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

Prerequisites for Logging Signals


Before starting to log signals:

1 Connect the Android device to a computer.


2 Create or open a Simulink model. To log signals, the model must have at least one of these
blocks.

Block Name Block Icon


To Workspace block

Outport block

3-3
3 MAT-File Logging on the Hardware

Configure Board Parameters and Enable MAT-File Logging


To save signals on the Android device, the device details must be specified, and the MAT-file logging
option must be selected.

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

Configure Model to Log Signals on the Android Device


MAT-file logging is supported in models containing To Workspace or Outport blocks. Specify the
values for several block parameters.

To configure a Simulink model to run on the Androiddevice, perform these steps:

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.

For example, if you specify Decimation as 5,


the block writes data at every fifth sample.
For example, if the block sample time is 0.1
and Decimation is 5, the data points at 0,
0.5, 1, 1.5, ... seconds are logged. The data
points are logged until the Simulation stop
time is reached.

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.

• Array: Save data as an array with


associated time information. This format
does not support variable-size data.
• Structure with Time: Save data as a
structure with associated time
information.
• Structure: Save data as a structure.
Sample time (–1 for inherited) Specify an interval at which the block reads
data. When you specify this parameter as –1,
the sample time is inherited from the driving
block.

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

3 Set the block parameters listed in this table:

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.

• Array: Save data as an array with


associated time information. This format
does not support variable-size data.
• Structure with Time: Save data as a
structure with associated time
information.
• Structure: Save data as a 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.

For example, if you specify Decimation as 5,


the block writes data at every fifth sample.
For example, if the block sample time is 0.1
and Decimation is 5, the data points at 0,
0.5, 1, 1.5, ... seconds are logged. The data
points are logged until the Simulation stop
time is reached.

3-10
Run Model on the Android Device

Run Model on the Android Device


To deploy the Simulink model on the Android device, in the model window, click the Build Model
button. The lower left corner of the model window displays status while Simulink prepares, and
downloads the application on the 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.

Note Data loss occurs when:

• 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

Import MAT-Files into MATLAB


After logging is complete, you can open MAT-files in MATLAB, and use them for further analysis.
Since the data points are stored in MAT-files, you can directly open the files in MATLAB without
converting them into any other format.

1 Copy the MAT-files from the Android device into MATLAB.

[variable1, variable2] = codertarget.android.internal.getFiles('modelname')

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] = codertarget.android.internal.getFiles('androidMatFile')

result =

matFiles =

'Successfully pulled following files from device:


androidMatFile_1_1.mat
androidMatFile_1_2.mat
androidMatFile_1_3.mat
androidMatFile_1_4.mat
androidMatFile_1_5.mat
androidMatFile_1_6.mat
androidMatFile_1_7.mat
androidMatFile_1_8.mat
'

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

You might also like