How to plot live data from Arduino UNO to App designer.

65 views (last 30 days)
Hi everyone,
I'm using an arduino UNO and MATLAB to plot sine wave points using serial communciation. My live data plotting in MATLAB script works perfectly and is shown below. However, I'm looking to use app designer to show the live data logging on the axes and where the app would only run when the 'Start' button is selected. I'm not too sure where to start, but I've tried to implment my matlab script code into app designer but have had no luck.
clear all;
close all;
serialPort = 'COM3';
plotTitle = 'Sine Wave';
xLabel = 'Time (s)';
yLabel = 'Data';
plotGrid = 'on';
min = -1.5;
max = 1.5;
delay = .01;
%Define Function Variables
time = 0;
data = 0;
count = 0;
%Set up Plot
plotGraph = plot(time,data,'-r');
axis([0 10 min max]);
%Open Serial COM Port
s= serial(serialPort);
disp('Close Plot to clear data');
% start stopwatch timer
%Loop when Plot is Active
while time <=50
%Read Data from Serial as Float
dat = fscanf(s,'%f');
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
count = count + 1;
time(count) = toc; %stop the stopwatch and extract time
data(count) = dat(1); %Extract 1st Data Element
axis([0 time(count) min max]);
%Allow MATLAB to Update Plot
%Close Serial COM Port and Delete useless Variables
clear count dat delay max min plotGraph plotGrid plotTitle s ...
serialPort xLabel yLabel;

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 4 Jun 2021
Edited: Benjamin Kraus on 4 Jun 2021
At a very high level:
  1. Create a new app in App Designer. See the tutorials linked below for how to get started.
  2. Add at least a button and an axes to the app.
  3. Add a callback to the button that runs your code.
  4. Update the code to always use an axes handle.
For an example of the last step, this block of your code:
plotGraph = plot(time,data,'-r');
axis([0 10 min max]);
Needs to be updated to look like this:
ax = app.UIAxes % Use the name you give your axes in your app.
plotGraph = plot(ax, time,data,'-r');
title(ax, plotTitle,'FontSize',18);
xlabel(ax, xLabel,'FontSize',15);
ylabel(ax, yLabel,'FontSize',15);
axis(ax, [0 10 min max]);
grid(ax, plotGrid);
For more details, see these doc pages:
If you run into complications after going through those doc pages, I suggest posting a more specific question about the specific complication you run into.
Benjamin Kraus
Benjamin Kraus on 14 Jun 2021
Edited: Benjamin Kraus on 14 Jun 2021
MATLAB opening a new figure is a good indication that you missed passing an axes handle into one line of code. In this case, it is the call to axis:
axis([0 time(count) min max]);
You need to pass an axes handle into the axis command.
axis(ax,[0 time(count) min max]);
If you see this issue again, my recommendation is to add a breakpoint at the beginning of your code, and step through the code one line at a time until the new figure appears. That is most likely the line that needs to be updated.

Sign in to comment.

More Answers (1)

Fazli Wadood
Fazli Wadood on 22 Mar 2023 at 9:24
To plot Analoge voltage using app UIaxis below code is working copy code and paste in push button callblock
global b
b = arduino;
global go
while go
tempA1 = readVoltage(b, 'A0');
tempA2 = 32+(9/5)*(tempA1*100);
x1=[x1 tempA2];


Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!