Repository navigation
MATLAB Functions
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.
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:
- Upload
serialComsEasy.inoto an Arduino - Identify the serial port and update the first argument in line 2
s = serial('/dev/cu.usbmodem14101', 'BaudRate', 115200); - Run
serialComsEasy.m
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:
- Upload
serialComsEfficient.inoto an Arduino - Identify the serial port and update the first argument in line 2
s = serial('/dev/cu.usbmodem14101', 'BaudRate', 9600);. - Run
serialComsEfficient.m
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.