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

8 visualizaciones (últimos 30 días)
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

Respuestas (1)

Swastik Sarkar
Swastik Sarkar el 18 de Jun. de 2025
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.

Categorías

Más información sobre Axes Appearance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by