9. Matlab

Welcome to the Matlab on Windows tutorial. This tutorial will give you the ability to log or stream data from a MetaSensor to Matlab.

9.1. Prerequsites

You are required to have access to Matlab with a current license for this tutorial. Typically, your school or university will have Matlab available to you for free. You can purchase and download Matlab here: https://www.mathworks.com/products/matlab.html

You should also be familiar with the MetaBase App for Windows. If you have not downloaded our free App yet, please refer to this section of the tutorial: https://mbientlab.com/tutorials/MetaBaseApp.html

Please note that our tutorial has only been tested on Windows 10 and Matlab R2018a.

Warning

Matlab does not support Bluetooth Low Energy and this tutorial provides a work around to send MetaSensor data to Matlab.

9.2. Log Temperature Data

Using the Windows MetaBase App, the MetaSensors is configured to log sensor data.

_images/matlab-11.jpg

Please refer to the MetaBase section of the tutorial if you need additional help.

_images/matlab-12.jpg

In this example, temperature on the MetaSensor was logged for a few minutes.

_images/matlab-14.jpg

As per our MetaBase on Windows tutorial, the resulting data is available in your Downloads Folder as a .csv file:

_images/matlab-15.jpg

Because the Matlab csv reader function csvread does not handle text very well, I removed the first two columns from the csv file with the epoch and timestamp data types using Excel. Only the time elapsed column will be used.

_images/matlab-16.jpg

The following commands are used to read the csv file into Matlab:

>>> Data=csvread('Temp-Matlab_MetaWear_2018-11-29T17.30.55.601_D8B511219606_Temperature_1s.csv',1,0);
>>> col1 = Data(:, 1);
>>> col2 = Data(:, 2);

As you can see, we read the csv file into the Data variable and we skip the first row because it is only text. The data is separated into the two columns: col1 is the time in seconds and col2 is the temperature data in degrees.

_images/matlab-17.jpg

Finally the data is plotted:

>>> plot(col1, col2)
_images/matlab-18.jpg

9.3. Stream Gyroscope Data

Our streaming examples use Python to send data through the Bluetooth link to the Windows PC and then pipe data to Matlab using a socket.

You are expected to have at least a basic understanding of Python as a language. You can go to the Python for Windows portion of the tutorial to learn more.

Download the examples from our Github: https://github.com/mbientlab/MetaWear-Tutorial-Matlab

You can clone the repository or simply download it. The Tutorial repository includes two python scripts (.py files) and two Matlab scripts (.m files).

We will first use the echo_gyro_server.py python script to stream MetaSensors gyroscope data live from the Bluetooth dongle on a Windows PC via a TCP socket into Matlab.

First Python needs to setup the socket and get the MetaSensor ready. On the command line, type:

>>> python echo_gyro_server.py D8:B5:11:21:96:06
_images/matlab-3.jpg

This Python script will open up a TCP socket on port 50007 of your local machine (Windows PC) and the script will wait until there is a response on the socket from Matlab.

Open up the real_time_gyro_data_stream_plotting.m file in Matlab.

_images/matlab-5.jpg

Hit the RUN button or type this into the command line of Matlab:

>>> real_time_gyro_data_stream_plotting
_images/matlab-1.jpg

The python script can now continue because the socket is open. The script configures the MetaSensors to stream accelerometer x,y,z data at 50Hz with the range set at 125dps:

_images/matlab-2.jpg

The gyroscope x,y,z data is available in the callback in Matlab and stored in FIFOs (asynchronous buffers).

When the Matlab script ends, the x,y,z gyroscope data is graphed using the plot command:

_images/matlab-4.jpg

9.4. Stream Accelerometer Data

Now we will use the echo_acc_server.py python script to stream MetaSensors accelerometer data live from the Bluetooth dongle on a Windows PC via a TCP socket into Matlab.

First Python needs to setup the socket and get the MetaSensor ready. On the command line, type:

>>> python echo_acc_server.py D8:B5:11:21:96:06
_images/matlab-3.jpg

This Python script will open up a TCP socket on port 50007 of your local machine (Windows PC) and the script will wait until there is a response on the socket from Matlab.

Open up the real_time_acc_data_stream_plotting.m file in Matlab.

Hit the RUN button or type this into the command line of Matlab:

>>> real_time_acc_data_stream_plotting

The python script can now continue because the socket is open. The script configures the MetaSensors to stream accelerometer x,y,z data at 100Hz with the range set at 2Gs.

The accelerometer x,y,z data is ploted live (in real-time) in the callback in Matlab:

_images/matlab-19.jpg

9.5. Stream Sensor Fusion Data

We will now use the echo_quat_server.py python script to stream MetaSensors Euler angles (roll, pitch, yaw) live from the Bluetooth dongle on a Windows PC via a TCP socket into Matlab.

First Python needs to setup the socket and get the MetaSensor ready. On the command line, type:

>>> python echo_quat_server.py D8:B5:11:21:96:06
_images/matlab-6.jpg

This Python script will open up a TCP socket on port 50007 of your local machine (Windows PC) and the script will wait until there is a response on the socket from Matlab.

Open up the real_time_quat_data_stream_plotting.m file in Matlab.

_images/matlab-7.jpg

Hit the RUN button or type this into the command line of Matlab:

>>> real_time_quat_data_stream_plotting
_images/matlab-8.jpg

The Python script can now continue because the socket is open. The script configures the MetaSensors to stream accelerometer roll, pitch, yaw data (in degrees) at 100Hz:

_images/matlab-10.jpg

The sensor fusion data is available in the callback and ploted live in 3D.

_images/matlab-9.jpg

As you move your MetaSensor around, you should see the sphere move in 3D on the plot as well.

We encourage you to play with this example and change the axes as needed.

Warning

Matlab can be slow and you may see delays when plotting/graphing data live.

If you look at the scripts, you will notice that you can uncomment a few specific lines and stream quaternion data instead of Euler angles. Feel free to give it a try.

9.6. Next steps

Go and build your own Matlab and Python script!

We welcome pull requests on our Matlab Tutorial Github Repository: https://github.com/mbientlab/MetaWear-Tutorial-Matlab