MATLAB Answers

Using animatedline in Matlab App Designer?

122 views (last 30 days)
Jeff Mays
Jeff Mays on 26 Jun 2019
Answered: Chris Portal on 26 Jun 2019
Hey everyone,
I am attempting to make a matlab app that is able to plot live data from my serial port. The data comes in separated with commas and is parsed accordingly (still in its early stages). I was able to get this code to work in the normal matlab code envirnment, shown below:
clear all;
delete(instrfindall);
clear;
close all;
clc;
s = serial('COM11', 'BaudRate',115200);
fopen(s);
voltage = 0;
t = 0;
y = 1;
data = fscanf(s);
data = convertCharsToStrings(data);
data = strsplit(data, ',');
TIME(y,1) = str2double(data(1));
VAR1(y,1) = str2double(data(2));
VAR2(y,1) = str2double(data(3));
h = animatedline(TIME(y,1),VAR1(y,1));
h2 = animatedline(TIME(y,1),VAR2(y,1));
xlim([TIME(y,1) TIME(y,1)+20]);
ylim([-1.2 1.2]);
grid on
title("Some Sine Waves");
xlabel("Time [sec]");
ylabel("Value");
tic
while t <= 200
y= y+1;
data = fscanf(s);
data = convertCharsToStrings(data);
data = strsplit(data, ',');
TIME(y,1) = str2double(data(1));
VAR1(y,1) = str2double(data(2));
VAR2(y,1) = str2double(data(3));
addpoints(h, TIME(y,1), VAR1(y,1));
addpoints(h2, TIME(y,1), VAR2(y,1));
xlim([TIME(y,1)-20 TIME(y,1)+4])
%y= y+1;
t=t+0.05;
drawnow
end
toc
fclose(s);
delete(s);
clear s;
This code works and will plot the data. I tried some other methods and the matlab program started to lag very badly, especially after running for 20+ seconds.
Using this code, I tried to apply it to the Matlab App Designer and this is where I am having trouble. The code "error" I am getting below is...
Reference to non-existent field 'UIAxes'
which makes no sense because that is my figure title. This happens right as I define the "animatedline" function. I know other people are having similar problems. When I push a button, the code gets executed. The code in my matlab app designer code window is below:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
Apollo matlab.ui.Figure
Button matlab.ui.control.Button
UIAxis matlab.ui.control.UIAxes
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
end
% Button pushed function: Button
function ButtonPushed(app, event)
clear all;
delete(instrfindall);
clear;
close all;
clc;
s = serial('COM11', 'BaudRate',115200);
fopen(s);
t = 0;
y = 1;
data = fscanf(s);
data = convertCharsToStrings(data);
data = strsplit(data, ',');
app.TIME(y,1) = str2double(data(1));
app.VAR1(y,1) = str2double(data(2));
app.VAR2(y,1) = str2double(data(3));
app.h = animatedline(app.UIAxis, app.TIME(y,1),app.VAR1(y,1));
app.h2 = animatedline(app.UIAxis, app.TIME(y,1),app.VAR2(y,1));
% xlim([TIME(y,1) TIME(y,1)+20]);
% ylim([-1.2 1.2]);
% grid on
% title("Some Sine Waves");
% xlabel("Time [sec]");
% ylabel("Value");
tic
while t <= 200
y= y+1;
data = fscanf(s);
data = convertCharsToStrings(data);
data = strsplit(data, ',');
TIME(y,1) = str2double(data(1));
VAR1(y,1) = str2double(data(2));
VAR2(y,1) = str2double(data(3));
addpoints(h, TIME(y,1), VAR1(y,1));
addpoints(h2, TIME(y,1), VAR2(y,1));
%xlim([TIME(y,1)-20 TIME(y,1)+4])
%y= y+1;
t=t+0.05;
drawnow
end
toc
fclose(s);
delete(s);
clear s;
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create Apollo
app.Apollo = uifigure;
app.Apollo.Position = [100 100 640 480];
app.Apollo.Name = 'UI Figure';
% Create Button
app.Button = uibutton(app.Apollo, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [45 428 100 22];
% Create UIAxis
app.UIAxis = uiaxes(app.Apollo);
title(app.UIAxis, 'SINE WAVE')
xlabel(app.UIAxis, 'X')
ylabel(app.UIAxis, 'Y')
app.UIAxis.PlotBoxAspectRatio = [1 0.571428571428571 0.571428571428571];
app.UIAxis.Position = [83 111 407 261];
end
end
methods (Access = public)
% Construct app
function app = app1
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.Apollo)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.Apollo)
end
end
end

  0 Comments

Sign in to comment.

Accepted Answer

Chris Portal
Chris Portal on 26 Jun 2019
Your callback is doing a “clear all”, which clears out all of the variables available at that point, which in this case are “app” and “event”. That’s why when you try to access “app.UIAxis” you get an error saying it doesn’t exist.
I don’t see why you would need to call clear in your callback function. It looks like you may have copied this from a script, where the call to clear was serving more of a purpose.
Hope this helps!

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.