Real Time Subplot Error

4 views (last 30 days)
Magnus354
Magnus354 on 20 Aug 2019
Hello,
I would like to create a realtime subplot with Matlab.
I get data from four different channels.
I have written the following code for it:
%%
Vref = 2.42;
n = 24;
gain = 12;
LSB = (2*Vref)/gain/((2^(24))-1);
%figure
figure('Renderer', 'painters', 'Position', [10 10 2000 500])
h1 = animatedline('Color','r','LineWidth',1);
h2 = animatedline('Color','r','LineWidth',1);
h3 = animatedline('Color','r','LineWidth',1);
h4 = animatedline('Color','r','LineWidth',1);
ax = gca;
ax.YGrid = 'on';
stop = false;
startTime = datetime('now');
while ~stop
% Read current voltage value
fprintf(bt, 'y');
Val1 = fscanf(bt, '%f');
Vout1 = Val1*LSB;
pause(5/100000);
fprintf(bt, 'c');
Val2 = fscanf(bt, '%f');
Vout2 = Val2*LSB;
pause(5/100000);
fprintf(bt, 'v');
Val3 = fscanf(bt, '%f');
Vout3 = Val3*LSB;
pause(5/100000);
fprintf(bt, 'b');
Val4 = fscanf(bt, '%f');
Vout4 = Val4*LSB;
subplot(4,1,1)
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h1,datenum(t), Vout1)
% Update axes
%ylim([-2 2])
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
subplot(4,1,2)
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h2,datenum(t), Vout2)
% Update axes
%ylim([-2 2])
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
subplot(4,1,3)
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h3,datenum(t), Vout3)
% Update axes
%ylim([-2 2])
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
subplot(4,1,4)
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h4,datenum(t), Vout4)
% Update axes
%ylim([-2 2])
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
Unfortunately I always get the error message:
"Invalid or deleted object.
Error in readbluetooth2 (line 208)
ax.XLim = datenum([t-seconds(15) t]);"
What am I doing wrong?

Answers (0)

Community Treasure Hunt

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

Start Hunting!