Clear Filters
Clear Filters

Two Subplots in an Animated For-loop Problem

2 views (last 30 days)
Stella
Stella on 31 Jan 2024
Commented: Stella on 1 Feb 2024
Hi! I am new to Matlab and trying to learn more each day, and have some issues regarding making both of my subplots play at the same time. For the first plot, I searched for the peaks and valleys and plot them as the data recognized them as peaks/valleys, and have no problem with it. However, the second plot that I want to show is the displacement gathered from the peaks and valleys and show them when the first plot passes through the peaks/valleys. I also don't know how to incorporate the code for it, and I only know to use "comet()" on it but still not what i am looking for. My problem is in the for loop I made. Can you help me? Thanks in advance!
figure(3);
subplot (2,1,1)
h = plot(time(1), mag(1)); hold on; % Start with the first data point
ylabel('Acceleration');
title('Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 1:200:numel (time)
x1 = time(1:i);
y1 = mag(1:i);
refreshdata(h, 'caller');
drawnow;
[pks, locs] = findpeaks (mag,'MinPeakDistance',1500);
[vks, vlocs] = findpeaks (-mag, 'MinPeakDistance',1300);
vks = -vks;
valid_vks_indices = vlocs (time(vlocs) <= x1 (end));
valid_pks_indices = locs (time(locs) <= x1 (end));
if ~isempty (valid_vks_indices)
plot (time(valid_vks_indices), vks(ismember(vlocs, valid_vks_indices)), 'or');
end
if ~isempty (valid_pks_indices)
plot (time(valid_pks_indices), pks (ismember(locs, valid_pks_indices)), 'or');
end
pause(0.001);
subplot (2,1,2);
xlabel ('Time'); ylabel('Displacement');
for j = i
vel = cumtrapz (time, mag);
vel = filtfilt (b2, a2, vel);
disp = cumtrapz (time, vel);
disp = filtfilt (b2, a2, disp);
plot (time, disp)
end
end
  4 Comments
Angelo Yeo
Angelo Yeo on 1 Feb 2024
Your code doesn't work. Can you check the following lines?
plot (time (vks), mag (ismember 'or')
plot (time, mag, x_pks, y_pks, 'or')
Stella
Stella on 1 Feb 2024
Oh sorry!! This is the updated one.
figure(3);
subplot (2,1,1)
h = plot(time(1), mag(1)); hold on; % Start with the first data point
ylabel('Acceleration');
title('Real-time Signal Analysis');
h.XDataSource = 'x1'; % Set the data source properties for dynamic updating
h.YDataSource = 'y1';
for i = 1:200:numel (time)
x1 = time(1:i);
y1 = mag(1:i);
refreshdata(h, 'caller');
drawnow;
[pks, locs] = findpeaks (mag,'MinPeakDistance',1500);
[vks, vlocs] = findpeaks (-mag, 'MinPeakDistance',1300);
vks = -vks;
valid_vks_indices = vlocs (time(vlocs) <= x1 (end));
valid_pks_indices = locs (time(locs) <= x1 (end));
if ~isempty (valid_vks_indices)
plot (time(valid_vks_indices), vks(ismember(vlocs, valid_vks_indices)), 'or');
end
if ~isempty (valid_pks_indices)
plot (time(valid_pks_indices), pks (ismember(locs, valid_pks_indices)), 'or');
end
pause(0.001);
subplot (2,1,2);
xlabel ('Time'); ylabel('Displacement');
for j = i
vel = cumtrapz (time, mag);
vel = filtfilt (b2, a2, vel);
disp = cumtrapz (time, vel);
disp = filtfilt (b2, a2, disp);
plot (time, disp)
end
end
hold off;

Sign in to comment.

Answers (0)

Categories

Find more on Visual Exploration 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!