Plot multiple results using for loop

조회 수: 5 (최근 30일)
rex a
rex a 2020년 5월 7일
답변: Deepak 2024년 11월 7일 7:20
I'm trying to plot raw and window 10, 50 and 100 all in one graph. I get:
Error using plot
Vectors must be the same length.
Error in moving_avg_filter (line 34)
plot(t,filtered_z)
When I ran the code with only plot(filtered_z). In the workspace it showed that filtered_z is 1x2296 so I tried to set t the same. However, when I ran it using plot(t,filtered_z) it didn't work and the values in the workspace changed to this:
Also, I tried placing the plot outside the for loop and it worked, but it will only give the last loop. So, how can I fix this?
Below is my code.
clear
clc
%You will need to add the logic for plotting all window
%sizes and FFT as instructed in the lab document
%Read data in
data = csvread('z_flip_data_60BPM.csv');
%Assign columns from csv
z_raw = data(:,2); %Raw signal from data collection
time = data(:,1)/1000; %time converted to seconds
plot(time,z_raw,'r');
hold on
%Change window size as required
window = [10 50 100];
%sanity check to avoid improper window size settings
if window>length(z_raw)
return;
end
%Indexing for the filtered array
j = 1;
t = time(1:2296,1);
%Moving average window filter + basic plot
for i = window+1:(length(z_raw))
filtered_z(j) = mean(z_raw(i-window:i));
j = j+1;
plot(t,filtered_z)
hold on
end

답변 (1개)

Deepak
Deepak 2024년 11월 7일 7:20
Hi @rex a,
To fix the issue of mismatched vector lengths when plotting the filtered signals, we need to ensure that the time vector matches the length of the filtered data for each window size. This can be achieved by creating a new “filtered_time” vector that starts at the index corresponding to the end of window, ensuring it aligns with the shorter “filtered_z” vector.
Next, by iterating over each window size and plotting the results within the loop, we can maintain all plots on the same graph.
Additionally, using the “DisplayName” property in the “plot” function allows for the dynamic labelling of each line, and calling the “legend show” after plotting displays a legend that helps distinguish between raw and filtered signals.
Please find below the updated MATLAB code with the required changes:
data = csvread('z_flip_data_60BPM.csv');
z_raw = data(:,2); % Raw signal from data collection
time = data(:,1)/1000; % Time converted to seconds
figure;
plot(time, z_raw, 'r');
hold on
windows = [10, 50, 100];
% Loop over each window size
for w = windows
% Initialize filtered_z for the current window size
filtered_z = zeros(1, length(z_raw) - w);
% Moving average window filter
for i = w+1:length(z_raw)
filtered_z(i-w) = mean(z_raw(i-w:i));
end
% Adjust time vector for plotting filtered data
filtered_time = time(w+1:end);
% Plot the filtered data
plot(filtered_time, filtered_z, 'DisplayName', sprintf('Window %d', w));
end
legend show
xlabel('Time (s)')
ylabel('Amplitude')
title('Raw and Filtered Signals')
hold off
Please find attached the documentation of functions used for reference:
I hope this assists in resolving the issue.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by