How can I change this code to get the line when plotting the graph? Because I tried adding commands related but still there is no line when I am running the codes

조회 수: 1 (최근 30일)
function [] = i2c_sensor()
a = arduino('COM3', 'UNO');
imu = mpu6050(a,'SampleRate',50,'SamplesPerRead',10,'ReadMode','Latest');
for i=1:10
accelReadings(i,:) = readAcceleration(imu);
display(accelReadings(i,:));
pause(1);
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
title('Acceleration values from mpu6050');
x_val = animatedline('Color','r');
y_val = animatedline('Color','g');
z_val = animatedline('Color','b');
end
end

채택된 답변

dpb
dpb 2021년 2월 2일
You're creating a new animatedline object every time, not adding points to the ones intended...that's not how the examples in the documentation show using it.
function [] = i2c_sensor()
a = arduino('COM3', 'UNO');
imu = mpu6050(a,'SampleRate',50,'SamplesPerRead',10,'ReadMode','Latest');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
title('Acceleration values from mpu6050');
hx_val = animatedline('Color','r');
hy_val = animatedline('Color','g');
hz_val = animatedline('Color','b');
for i=1:10
accelReadings(i,:) = readAcceleration(imu);
t(i)=????
display(accelReadings(i,:));
addpoints(hx_val(t(i),accelReadings(i,1));
addpoints(hy_val(t(i),accelReadings(i,2));
addpoints(hz_val(t(i),accelReadings(i,3));
drawnow
pause(1);
end
end
ASSUMPTIONS:
  1. x,y,z components of acceleration are in a 3-vector returned from the readAcceleration routine,
  2. can return the sample time for each reading in some manner into variable t which is needed for an abscissa plotting location by addpoints
Read the documentation for both animatedline and addpoints and look at all the examples carefully for further enhancements in using.
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 2월 3일
addpoints(hx_val, t(i),accelReadings(i,1));
addpoints(hy_val, t(i),accelReadings(i,2));
addpoints(hz_val, t(i),accelReadings(i,3));

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by