What can I add in this code for me to plot the data that I obtained in real time graph?

9 次查看(过去 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

回答(1 个)

Swastik Sarkar
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.

类别

Help CenterFile Exchange 中查找有关 Axes Appearance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by