Skip to content

MATLAB Functions

Mark Sherstan edited this page Dec 25, 2019 · 1 revision

The following examples are extremely helpful for dealing with realtime data over a serial port in MATLAB. For a variety of other MATLAB questions and answers checkout my page here.

Serial Coms Easy

Uses the Arduino serial port and MATLAB's built in functions to handle a serial port connection. It is easy to use and understand but is inefficient and has slow data rates. The slow rates are due to MATLAB and the Arduino exchanging data as ASCII characters instead of their actual type. This issue is fixed with the Serial Coms Efficient code.

To use follow the steps below:

  1. Upload serialComsEasy.ino to an Arduino
  2. Identify the serial port and update the first argument in line 2 s = serial('/dev/cu.usbmodem14101', 'BaudRate', 115200);
  3. Run serialComsEasy.m

Serial Coms Efficient

To fix the data rate limitations of the serial coms easy example the values are transmitted as raw bytes instead of ASCII characters. The example code assumes that the data is of type int but the code can easily be adapted to double, floats, longs, or otherwise by changing the corresponding number of bytes for the data type in both the Arduino sketch and MATLAB script.

To use follow the steps below:

  1. Upload serialComsEfficient.ino to an Arduino
  2. Identify the serial port and update the first argument in line 2 s = serial('/dev/cu.usbmodem14101', 'BaudRate', 9600);.
  3. Run serialComsEfficient.m

Real Time Plotter

If using realtime data, MATLAB's plotting options are somewhat limited (2019). The custom function mimics the realtime plotter of the Arduino IDE with a moving x-axis. Just adjust the v = randi(4); variable to your realtime data and adjust the y-axis as required.

Home

MATLAB Functions

Mint Automation

Misc Arduino Sensors

openCV

PoGo

PWM

Python Sleep

Serial Port

Soil Humidity

Stocks

Clone this wiki locally