What can I add in this code for me to plot the data that I obtained in real time graph?
4 ビュー (過去 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!
