Plotting animated plots with peaks and valleys problem
Show older comments
Hi!! I have a problem that I can't do while using a for loop command for showing the delayed peaks and valleys. So far I plotted them in one graph, but at the beginning, the peaks and valleys are showed already in my plot. I wanted them to be shown one at a time (like the attached video) until it reached the end of the data. I tried using the pause command and while loop, but still not working. Am I doing something wrong or my code is incomplete? Also, thank you in advance!!!
P.S. I am still new to matlab, so I am sorry for my dumb question.
%raw data from XYZ direction
NormChest = readtable('TV_500mL.csv'); %%%get the raw data
C = table2array(NormChest(:,1));
x = table2array(NormChest(:,2));
y = table2array(NormChest(:,4));
z = table2array(NormChest(:,6));
total = table2array(NormChest(:,8));
time = C(:,1);
% get relative acceleration
x = x - mean(x);
y = y - mean(y);
z = z - mean(z);
total = total - mean(total);
save time.mat time
clear time
load time.mat
%design for low pass filter
fs = 1000; %sampling frequency
fc = 1; %cut-off frequency
order = 2;
[b1, a1] = butter (order, fc/(fs/2));
%design for high pass filter
fcv = 0.1;
orderv = 1;
[b2, a2] = butter (orderv, fcv*2/fs, "high");
% magnitude computation
mag = sqrt(x.^2+y.^2+z.^2);
figure (2)
plot (mag); axis tight;
%mean value to 0
mag = mag - mean (mag);
mag = filtfilt (b1, a1, mag);
% peak and valley
[pks, locs] = findpeaks (mag, "MinPeakDistance",2000);
[vks, vlocs] = findpeaks(-mag, "MinPeakDistance",2000);
vks = -vks;
% Initialize the plot
n = time
figure(2);
h = plot(time(1), mag(1)); hold on; % Start with the first data point
xlabel('Time'); axis tight;
title('Subject 1 (500 mL) - Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 2:length(n)
% Update the data source
x1 = time(1:i*500);
y1 = mag(1:i*500);
refreshdata(h, 'caller');
drawnow;
plot (time (locs), pks, 'or');
plot (time (vlocs), vks, 'or');
pause(0.01); % Can be adjusted
end
hold off;
Accepted Answer
More Answers (0)
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!