필터 지우기
필터 지우기

Can't draw two plots with animated lines?

조회 수: 3 (최근 30일)
up_pro
up_pro 2019년 5월 31일
답변: up_pro 2019년 6월 7일
I was trying to design a program to receive a data and plot the real time information. This is my first time recording real time information. I needed two plots one with raw data and another with filtered data. But I can't even get the two graph to show the same information. Please help!!!
Error Message: Invalid or deleted object.
Error in trial2 (line 28)
ax.XLim = datenum([t-seconds(15) t]);
My code:
%% Clear all previous information from arduino board:
clc;
clear;
close all;
%% Use the arduino command to connect to an Arduino device.
a = arduino;
writeDigitalPin(a, 'D10', 1);
%% Receiving signal from Arduino and plotting the live data:
figure
h1 = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.YLim = [-5 5];
stop = false;
startTime = datetime('now');
while ~stop
v = readVoltage(a,'A0');
v2 = readVoltage(a,'A0')-1;
t = datetime('now') - startTime;
addpoints(h1,datenum(t),v)
ax.XLim = datenum([t-seconds(15) t]);
subplot(2,1,1)
xlabel('Elapsed time (sec)')
title('Raw Signal')
drawnow
subplot(2,1,2)
xlabel('Elapsed time (sec)')
title('Filtered Signal')
drawnow
datetick('x','keeplimits')
stop = readDigitalPin(a,'D3');
end
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2019년 5월 31일
up_pro - on which iteration of the loop did this error occur? Did you manually close the figure? Which may explain the invalid axes object? (I'm guessing that it is the axes that has become invalid...)
up_pro
up_pro 2019년 5월 31일
편집: up_pro 2019년 5월 31일
No, I didn't. The plot didn't display anything. and then the error msg appeared. It works perfectly fine when I only have one plot but with two, the error msg appears. My code with one plot is:
clc;
clear;
close all;
%% Use the arduino command to connect to an Arduino device.
a = arduino;
writeDigitalPin(a, 'D10', 1);
%% Receiving signal from Arduino and plotting the live data:
figure
h = animatedline;
ax = gca;
ax.YGrid = 'on';
ax.YLim = [-5 5];
stop = false;
startTime = datetime('now');
while ~stop
% Read current voltage value
v = readVoltage(a,'A0');
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h,datenum(t),v)
% Update axes
ax.XLim = datenum([t-seconds(5) t]);
datetick('x','keeplimits')
xlabel('Elapsed time (sec)')
ylabel('Voltage (micro-V)')
title('Raw Signal')
drawnow
% Check stop condition
stop = readDigitalPin(a,'D3');
end

댓글을 달려면 로그인하십시오.

채택된 답변

up_pro
up_pro 2019년 6월 7일
Solved the problem:
figure
subplot(2,1,1)
h1 = animatedline;
ax1 = gca;
ax1.YGrid = 'on';
ax1.YLim = [-5 0];
subplot (2,1,2)
h2 = animatedline;
ax2 = gca;
ax2.YGrid = 'on';
ax2.YLim = [0 5];
stop = false;
startTime = datetime('now');
while ~stop
% Read current voltage value
v = -readVoltage(a,'A0');
v_rms = rms(v);
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h1,datenum(t),v)
addpoints(h2,datenum(t),v_rms)
% Update axes
% Update axes
ax1.XLim = datenum([t-seconds(15) t]);
ax2.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
xlabel('Elapsed time (sec)')
ylabel(ax1,'Voltage (micro-V)')
ylabel(ax2,'Voltage (micro-V)')
title(ax1,'Raw Signal')
title(ax2,'rms signal')
drawnow
% Check stop condition
stop = readDigitalPin(a,'D3');
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by