Plotting data at a fast rate in app designer

2 views (last 30 days)
Juha Forssell
Juha Forssell on 17 May 2021
Answered: Mohammad Sami on 18 May 2021
Hey,
I have been trying to use app designer to plot data from a file. The file's data is originally collected with a 50 Hz sample rate. Is it at all possible to plot the data this fast in app designer. In the code I have now the frequency of the plotting can be changed by changing the period in the timer object creation. However, my computer struggles to plot the data even at a 0.5 second rate. Is there any way of plotting the data in a different way that allows for a faster frequency? So my code works, but it really isn't useful if I cannot plot at a higher frequency. There might be some parts in the code that are redundant such as the variable z, but you can ignore those.
All (hopefully) relevant parts of the code below.
properties (Access = private)
aTimer % Timer object
A
ii
end
methods (Access = private)
function aTimerFcn(app, ~, event)
% get current time from timer's event data
tnow = datetime(event.Data.time);
app.ii = app.ii+1;
A1 = app.A(app.ii,:);
matrix = reshape(A1,[4,4]);
y = rand();
z = y+1;
%cdata = rand(4,4);
%data =rand(1,8);
% Plot data and adjust XLim if current time is out of bounds
xLL = app.UIAxes.XLim(1);
xUL = app.UIAxes.XLim(2);
if(tnow > xUL)
xtra = seconds(15);
app.UIAxes.XLim = [xLL+xtra tnow+xtra];
app.UIAxes.XTick = linspace(xLL+xtra, tnow+xtra, 5);
end
plot(app.UIAxes, tnow, A1,'LineStyle',"-", "Marker","."); %, tnow, z, 'oy');
heatmap(app.MatrixPanel, matrix, 'Colormap',parula,'CellLabelColor','none','ColorLimits',[410 420]);
% Adjust YLim to show variations better
yLL = 400; %max(y-5, 0);
yUL = 430; %y + 5;
app.UIAxes.YLim = [yLL yUL];
%app.UIAxes.YTick = round(linspace(yLL, yUL, 5), 2, 'significant');
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.AAUIFigure.Name = 'AA Project V0.1 01APR21';
% Plot invisible data point to initialize datetime ruler
plot(app.UIAxes, datetime('now'), 50,'ob');
cdata = rand(8,8);
app.A = readmatrix('A.txt','FileType','text');
heatmap(app.MatrixPanel, cdata);
% Set x limits and ticks
t = datetime('now') + seconds(1:60);
app.ii = 0;
xLL = t(1) - seconds(5);
xUL = t(length(t));
app.UIAxes.XLim = [xLL xUL];
app.UIAxes.XAxis.TickLabelFormat = 'hh:mm:ss';
app.UIAxes.XTick = linspace(xLL, xUL, 5);
% Set y limits and ticks
app.UIAxes.YLim = [0 50];
app.UIAxes.YTick = round(linspace(0, 50, 5), 2, 'significant');
grid(app.UIAxes,'on');
hold(app.UIAxes,'on');
% Create timer object
app.aTimer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Period is 1 second
'BusyMode', 'queue',... % Queue timer callbacks when busy
'TimerFcn', @app.aTimerFcn); % Callback that runs every period
end

Answers (1)

Mohammad Sami
Mohammad Sami on 18 May 2021
Similar to how you are storing UIAxes in the app properties you need to store the plot and the heatmap in the properies as well. Then you can update them in your timer function. That should speed up your code.
properties (Access = private)
aTimer % Timer object
A
ii
dataplot
heatmapplot
end
function startupFcn(app)
%....%
app.heatmapplot = heatmap(app.MatrixPanel, cdata,'Colormap',parula,'CellLabelColor','none','ColorLimits',[410 420]);
%....%
app.dataplot = plot(app.UIAxes, datetime('now'), 50,'LineStyle',"-", "Marker",".");
%....%
end
Then in timer function you can directly update the map and the plot.
function aTimerFcn(app, ~, event)
%....%
app.heatmapplot.ColorData = matrix;
%....%
app.dataplot.XData = tnow;
app.dataplot.YData = A1;
end

Community Treasure Hunt

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

Start Hunting!