Send Data from Arduino to MATLAB Using Custom Library
R2026bThis example shows how to send a string from an Arduino® Uno board to the MATLAB® command line using a custom Arduino library.
Generate Library Template
Use arduinoio.customLibrary.createLibraryTemplate to generate to create
a template for a custom library named HelloWorld.
arduinoio.customLibrary.createLibraryTemplate("HelloWorld")MATLAB creates the folder
+arduinoioaddons/+HelloWorldFolder containing
HelloWorld.m and a src folder with
HelloWorld.h. The generated template includes a built-in
testRead method that returns "Hello from
Arduino" from the board without any modifications.
Test the Template
You can immediately test the generated template to verify communication between MATLAB and the Arduino board.
Create an arduino object and include the new library.
Set ForceBuildOn to true to reprogram
the board.
arduinoObj = arduino('COM3', 'Uno', 'Libraries', 'HelloWorldFolder/HelloWorld', 'ForceBuildOn', true);Arduino devices reuse cached code if the specified library matches a library name in the source code. Reprogramming forces the device to download the latest header file.
Create an add-on object using the HelloWorld
library.
dev = addon(a,'HelloWorldFolder/HelloWorld');Call the built-in testRead method to execute the
command on the server, and read the data back into MATLAB.
testRead(dev)
ans =
'Hello from Arduino'See Also
arduinoio.customLibrary.createLibraryTemplate | arduinoio.customLibrary.downloadLibrary | addon | arduino