What can I add in this code for me to plot the data that I obtained in real time graph?
조회 수: 8 (최근 30일)
이전 댓글 표시
function [] = i2c_sensor()
a = arduino('COM3', 'UNO');
imu = mpu6050(a,'SampleRate',50,'SamplesPerRead',10,'ReadMode','Latest');
for i=1:10
accelReadings = readAcceleration(imu);
display(accelReadings);
pause(1);
end
end
댓글 수: 0
답변 (1개)
Swastik Sarkar
2025년 6월 18일
To plot real-time data from the sensor in MATLAB, the animatedline function can be used to dynamically update the graph as new data is read. Below is an example of how to implement this (using random values for acceleration):
figure;
hold on;
grid on;
title('Simulated Real-Time Acceleration Data');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
hX = animatedline('Color', 'r', 'DisplayName', 'X');
hY = animatedline('Color', 'g', 'DisplayName', 'Y');
hZ = animatedline('Color', 'b', 'DisplayName', 'Z');
legend;
startTime = datetime('now');
for i = 1:100
accelReadings = readAcceleration();
t = datetime('now') - startTime;
t = seconds(t);
addpoints(hX, t, accelReadings.X);
addpoints(hY, t, accelReadings.Y);
addpoints(hZ, t, accelReadings.Z);
drawnow;
pause(0.1);
end
function accel = readAcceleration()
accel.X = 2 * rand() - 1;
accel.Y = 2 * rand() + 1;
accel.Z = 2 * rand() + 3;
end
For more information on animatedLine, refer to the following documentation:
Hope this helps measure and plot as and when data is available.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Axes Appearance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!