Error in for loop (issue with variable calling)

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
Error using importdata
Unable to open file.

 채택된 답변

Voss
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
t_new = 81×1
0.0684 0.1367 0.2051 0.2637 0.3320 0.4004 0.4688 0.5371 0.5957 0.6641

댓글 수: 2

AL
AL 2023년 3월 15일
Dear @Voss Thank you so much for your effort. I really appricate it. Have a wonderful week.
Voss
Voss 2023년 3월 16일
You're welcome! Have a good one.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
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

AL
AL 2023년 3월 15일
Can please explain me, what have you changed?
This line:
z1 = importdata("Xxsv0000"+num2str(i)+".txt");
AL
AL 2023년 3월 15일
my code is working property and importing the data. I am facing error in line
t_new = Time{i}; % contains 1024 values (which is wrong) it shoud be 81x1
David Hill
David Hill 2023년 3월 15일
편집: David Hill 2023년 3월 15일
Your text file has 1024 rows of data. The above code is working properly. You need to explain why it should only be 81x1.
The ‘t_new’ cell array should be indexed by the index vector ‘Xpk’
t_new = Time{i}(Xpk)
That will produce the (81x1) vector.
.
Motor_plate = 17.010;
First_Story = 2.162;
Sec_Story = 2.162;
Top_strory = 1.790;
L_leg = 0.278;
S_Leg = 0.042;
bracket =19.75/1000;
boltnut =12.45/1000 ;
%Unbalance_mass = 0.044;
m1 = Motor_plate + 4*L_leg + 4*S_Leg + First_Story+40*bracket+64*boltnut;
m2 = 4*S_Leg+ First_Story+16*bracket+16*bracket;
m_for_k = [m1 m2];
M1 = Motor_plate + 2*S_Leg +2*L_leg ; %In kgs
M2 = First_Story+ 4*S_Leg;
M3 = Sec_Story + 4 * S_Leg;
M4 = Top_strory+2*S_Leg;
V = [M1 M2 M3 M4];
M = diag(V); % in Kgs
clear M1 M2 M3 M4 m1 m2 Motor_plate First_Story Sec_Story S_Leg;
clear Top_strory V L_leg bracket boltnut
for i = 1:2
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};
Log_Dec = abs(log(y_new(1:end-1) ./ y_new(2:end)));
Mean_dec = mean(Log_Dec);
Damp_period = (t_new(end) - t_new(1)) / length(t_new);
wd(i) = 2*pi / Damp_period;
damp_ratio(i) = Mean_dec / sqrt(1 + Mean_dec^2);
wn(i) = wd(i) * sqrt(1 - damp_ratio(i)^2);
% calculate spring constant
K_ex(i) = m_for_k * wn(i)^2;
end
I am facing issue with K_ex(i)
I am unable to find the mistake. error is due to size difference in K_ex(i). Hoping piror steps all are correct.
Can you please help me with this?
regards,
AL
@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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

AL
2023년 3월 15일

댓글:

2023년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by