Storing Real-Time Data from Arduino in a Single column Matrix

6 vues (au cours des 30 derniers jours)
Aaron Lad
Aaron Lad le 7 Mar 2022
Modifié(e) : Chetan le 28 Déc 2023
Hi Guys,
I'll preface this by saying I have little experience coding in MATLAB. Below is my code so far:
clear
clc
a = arduino('/dev/cu.usbmodem1301','Nano33IoT');
max_data_len = 100;
fs = 1e3;
t = 0:1/fs:1;
ECG_mat = zeros( max_data_len, 1);
while(1)
for i=1:60
v = readVoltage(a, 'A0');
ECG_mat(i,:) = [i,v];
pause(0.01);
bandpass(ECG_mat,[0.8 5], fs)
end
end
My aim is to read the data coming from the Arduino, store the data for a minute in a single column matrix and then pass this data through a filter. I then want to clear the data in the matrix and store the next set of data in the same matrix, filter and then plot with the first set of data and for this to continue until stopped by the user, however, I have no idea where to go from what I've got at the moment.
Any help would be greatly appreciated!
Aaron

Réponses (1)

Chetan
Chetan le 28 Déc 2023
Modifié(e) : Chetan le 28 Déc 2023
It Seems like you are trying to continuously read ECG data from an Arduino using MATLAB, apply a bandpass filter to the data, and plot the filtered data every minute.
To achieve your goal, you can modify your code as follows:
1. Initialize a figure outside the loop for plotting.
2. Collect data for one minute by reading from the Arduino inside the loop.
3. Apply the bandpass filter to the collected data.
4. Plot the filtered data.
5. Clear the data in the matrix for the next iteration.
Here's the modified code:
clear
clc
a = arduino('/dev/cu.usbmodem1301','Nano33IoT');
% Define sampling frequency and time vector
fs = 1e3; % 1000 Hz sampling frequency
t = 0:1/fs:59; % 60 seconds of data
% Initialize the matrix to store ECG data
ECG_mat = zeros(60*fs, 1);
% Initialize the figure for plotting
figure;
hold on;
% Loop to continuously collect data until stopped by the user
while (1)
% Collect data for one minute
for i = 1:length(t)
v = readVoltage(a, 'A0');
ECG_mat(i) = v;
pause(1/fs); % Pause for the sampling period
end
% Apply bandpass filter to the collected data
filtered_ECG = bandpass(ECG_mat, [0.8 5], fs);
% Plot the filtered data
plot(t, filtered_ECG);
drawnow; % Update the plot
% Clear the matrix for the next set of data
ECG_mat = zeros(60*fs, 1);
end
For more information on plotting in MATLAB and using the Arduino support package, you can refer to the following MathWorks documentation:
I hope these suggestions help you resolve the issue you are facing.
Best regards,
Chetan Verma

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by