Error in for loop (issue with variable calling)
1 次查看(过去 30 天)
显示 更早的评论
I am trying to find natural frquency for two sets of data. For that we have to follow this method

I am facing issue with calculation of Wd and Wn where i have to call values of Time from specific range.
for i = 1:1
z1 = importdata("XXsv0000"+i+".txt");
Time{i} = z1.data(:,1); % select the first column of data from rows defined in ind as time data
Veloc{i} = z1.data(:,2); % select the second column of data from rows defined in ind as velocity data
ind = 1:550; % redefine the range of indices to select data from Veloc
% find local maxima in Veloc data within the range of indices defined in ind
% returns peak amplitude (Ypk), peak index (Xpk), peak width (Wpk), and peak prominence (Ppk)
[Ypk,Xpk,Wpk1,Ppk] = findpeaks(Veloc{i}(ind));
Ypk_plot{i} = Ypk(11:35);
Xpk_plot{i} = Xpk(11:35);
Y{i} = Ypk; X{i} = Xpk; W{i} = Wpk1; P{i} = Ppk;
figure(i)
plot(Time{i},Veloc{i},Time{i}(Xpk_plot{i}),Ypk_plot{i},'dr'); % plot data against time data and mark the locations of the peaks found
grid on
legend(["data","Pick points"])
sgtitle("File " + string(i))
n_peaks = numel(X{i});
Log_Dec = zeros(1, n_peaks-1);
t_new = Time{i}; % contains 1024 values (which is wrong) it shoud be 81x1
y_new = Y{i};
for nn = 1:n_peaks-1 %
Log_Dec(nn) = log(y_new(nn)/y_new(nn+1)); % computed with n = 1 periods apart
end
%Calculate Average Logarithmic Decrement
Mean_dec = mean(Log_Dec);
%Assesses Damping Constant
damp_ratio(1,i) = 1/sqrt(1+((2*pi/(Mean_dec))^2));
end
0 个评论
采纳的回答
Voss
2023-3-15
Time{i} is all the times from the file. To get the times of the peaks found by findpeaks, index Time{i} with the peak locations X{i}. Also, the name of the file was misspelled.
for i = 1:1
% z1 = importdata("XXsv0000"+i+".txt");
z1 = importdata("Xxsv0000"+i+".txt");
Time{i} = z1.data(:,1); % select the first column of data from rows defined in ind as time data
Veloc{i} = z1.data(:,2); % select the second column of data from rows defined in ind as velocity data
ind = 1:550; % redefine the range of indices to select data from Veloc
% find local maxima in Veloc data within the range of indices defined in ind
% returns peak amplitude (Ypk), peak index (Xpk), peak width (Wpk), and peak prominence (Ppk)
[Ypk,Xpk,Wpk1,Ppk] = findpeaks(Veloc{i}(ind));
Ypk_plot{i} = Ypk(11:35);
Xpk_plot{i} = Xpk(11:35);
Y{i} = Ypk; X{i} = Xpk; W{i} = Wpk1; P{i} = Ppk;
figure(i)
plot(Time{i},Veloc{i},Time{i}(Xpk_plot{i}),Ypk_plot{i},'dr'); % plot data against time data and mark the locations of the peaks found
grid on
legend(["data","Pick points"])
sgtitle("File " + string(i))
n_peaks = numel(X{i});
Log_Dec = zeros(1, n_peaks-1);
% t_new = Time{i}; % contains 1024 values (which is wrong) it shoud be 81x1
t_new = Time{i}(X{i})
y_new = Y{i};
for nn = 1:n_peaks-1 %
Log_Dec(nn) = log(y_new(nn)/y_new(nn+1)); % computed with n = 1 periods apart
end
%Calculate Average Logarithmic Decrement
Mean_dec = mean(Log_Dec);
%Assesses Damping Constant
damp_ratio(1,i) = 1/sqrt(1+((2*pi/(Mean_dec))^2));
end
更多回答(1 个)
David Hill
2023-3-15
for i = 1:1
z1 = importdata("Xxsv0000"+num2str(i)+".txt");
Time{i} = z1.data(:,1); % select the first column of data from rows defined in ind as time data
Veloc{i} = z1.data(:,2); % select the second column of data from rows defined in ind as velocity data
ind = 1:550; % redefine the range of indices to select data from Veloc
% find local maxima in Veloc data within the range of indices defined in ind
% returns peak amplitude (Ypk), peak index (Xpk), peak width (Wpk), and peak prominence (Ppk)
[Ypk,Xpk,Wpk1,Ppk] = findpeaks(Veloc{i}(ind));
Ypk_plot{i} = Ypk(11:35);
Xpk_plot{i} = Xpk(11:35);
Y{i} = Ypk; X{i} = Xpk; W{i} = Wpk1; P{i} = Ppk;
figure(i)
plot(Time{i},Veloc{i},Time{i}(Xpk_plot{i}),Ypk_plot{i},'dr'); % plot data against time data and mark the locations of the peaks found
grid on
legend(["data","Pick points"])
sgtitle("File " + string(i))
n_peaks = numel(X{i});
Log_Dec = zeros(1, n_peaks-1);
t_new = Time{i}; % contains 1024 values (which is wrong) it shoud be 81x1
y_new = Y{i};
for nn = 1:n_peaks-1 %
Log_Dec(nn) = log(y_new(nn)/y_new(nn+1)); % computed with n = 1 periods apart
end
%Calculate Average Logarithmic Decrement
Mean_dec = mean(Log_Dec);
%Assesses Damping Constant
damp_ratio(1,i) = 1/sqrt(1+((2*pi/(Mean_dec))^2));
end
7 个评论
Star Strider
2023-3-16
@AL — I just now saw this.
I see the problem, and the solution is therefore straightforward. However you already accepted another Answer, so I will not post my solution.
另请参阅
类别
Find more on Live Scripts and Functions 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!