Real time plotting of data from csv file

9 visualizzazioni (ultimi 30 giorni)
Kishor Kumar
Kishor Kumar il 22 Lug 2020
Risposto: Deepak il 17 Ott 2024
Hello everyone,
I am stuck in one small problem. I am reading data from some sensor and capturing into csv file using terminal. How can I plot that data in real time by continuously reading file. Any lead will be helpful.
Thanks!

Risposte (1)

Deepak
Deepak il 17 Ott 2024
Hi Kishor,
From my understanding, you are reading the data from a sensor and capturing it into a CSV file. You want to plot the data continuously from the CSV file in real time.
To achieve this, we can first set up the MATLAB environment by defining the CSV file path and figure. Now, we can iterate over a loop to continuously read the data from the CSV file using “readmatrix” function and plot it using the “plot” function in MATLAB. As the size of CSV file continues to increase over time, updated plots will get generated in real time.
Here is the MATLAB code to accomplish this task:
% Define the file path
filePath = 'your_sensor_data.csv';
figure;
while true
% Read the CSV file
data = readmatrix(filePath);
% Assuming the data has two columns, time and sensor value
time = data(:, 1);
sensorValue = data(:, 2);
plot(time, sensorValue);
xlabel('Time');
ylabel('Sensor Value');
title('Real-Time Sensor Data');
grid on;
% Pause for a short time to allow the plot to update
pause(1);
% Clear the plot for the next update
clf;
end
Please find attached the documentation of functions used for reference:
I hope you found the information insightful.

Community Treasure Hunt

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

Start Hunting!

Translated by