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

56 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;
delete(instrfindall);
clear;
close all;
clc;
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');
title(plotTitle,'FontSize',18);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s= serial(serialPort);
disp('Close Plot to clear data');
fopen(s);
% start stopwatch timer
tic
%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
set(plotGraph,'XData',time,'YData',data);
axis([0 time(count) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
%Close Serial COM Port and Delete useless Variables
fclose(s);
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');
title(plotTitle,'FontSize',18);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
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.
  2 Comments
Benjamin Kraus
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 (0)

Community Treasure Hunt

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

Start Hunting!

Translated by