Why the for-loop doesn't work here?

1 次查看(过去 30 天)
Wiqas Ahmad
Wiqas Ahmad 2021-5-11
评论: KSSV 2021-5-11
close all;
clear all;
clc;
%% ------------------------------Program-------------------------------------
z=2860:11:3135;
FOV=[1 2];
i_in = getsignal(['FOV_',num2str(FOV(1)),'mrad\out_resultsG_I0.dat']);
q_in = getsignal(['FOV_',num2str(FOV(1)),'mrad\out_resultsG_Q0.dat']);
i_out = getsignal(['FOV_',num2str(FOV(2)),'mrad\out_resultsG_I0.dat']);
q_out = getsignal(['FOV_',num2str(FOV(2)),'mrad\out_resultsG_Q0.dat']);
I_in = sum(i_in,2);
Q_in = sum(q_in,2);
I_out = sum(i_out,2);
Q_out = sum(q_out,2);
dep_in = (I_in-Q_in)./(I_in+Q_in);
dep_out = (I_out-Q_out)./(I_out+Q_out);
figure,
hold on
plot(dep_in,z);
plot(dep_out,z);
figure,
hold on
plot(dep_in./dep_out,z);
I converted this program into for-loop as:
close all;
clear all;
clc;
%% ------------------------------Program-------------------------------------
z=2860:11:3135;
FOV=[1 2];
for i=1:length(FOV)
I0 = getsignal(['FOV_',num2str(FOV(i)),'mrad\out_resultsG_I0.dat']);
Q0= getsignal(['FOV_',num2str(FOV(i)),'mrad\out_resultsG_Q0.dat']);
I(i) = sum(I0(i),2);
Q(i) = sum(Q0(i),2);
dep(i) = (I(i)-Q(i))./(I(i)+Q(i));
figure,
hold on
plot(dep(i),z);
end
figure,
hold on
plot(dep(1)./dep(2),z);
The program has no error but now it doesn't display the plots. I need some correction to this program

回答(1 个)

KSSV
KSSV 2021-5-11
You save all the values into an array and plot after the for-loop.
%% ------------------------------Program-------------------------------------
z=2860:11:3135;
n = length(FOV) ;
FOV=[1 2];
I = zeros(1,n) ;
Q = zeros(1,n) ;
dep = zeros(1,n) ;
for i=1:n
I0 = getsignal(['FOV_',num2str(FOV(i)),'mrad\out_resultsG_I0.dat']);
Q0= getsignal(['FOV_',num2str(FOV(i)),'mrad\out_resultsG_Q0.dat']);
I(i) = sum(I0(i),2);
Q(i) = sum(Q0(i),2);
dep(i) = (I(i)-Q(i))./(I(i)+Q(i));
end
figure,
plot(dep,z);

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by