- https://www.mathworks.com/help/matlab/ref/plot.html
- https://www.mathworks.com/hardware-support/arduino-matlab.html
Storing Real-Time Data from Arduino in a Single column Matrix
6 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Chetan
2023-12-28
编辑:Chetan
2023-12-28
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!