Wrong code. Does not get into the while loop.
1 回表示 (過去 30 日間)
古いコメントを表示
Hi all,
I wrote a code which is not yielding what I epected. In the mat file, I have a variable called "UntitledVoltage" and it is the voltage gathered from a power relay. It looks like 28 28 28 28 28 0 0 0 0 0 0 28 28 28 28 28 0 0....(a lot of values here, and at the end the voltage decreases like:) 20 20 20 20 20 0 0 0 0 0 20 20 20 20 20 0 0 0 0 0.
So here it is the code I wrote:
clear load('Voltage1.mat')
Arc=zeros(numel(UntitledVoltage),1);
MeanVolt=zeros(numel(UntitledVoltage),1);
OpenVoltage=zeros(100,1);
s=0;
n=0;
t=1;
x=1;
m=1;
for k=2:numel(UntitledVoltage)
s=k; %so the while does not occur after a consecutive 1 and 2nd condition
occurs as the k reiniciates after the while loop.
disp(k) It displays as it is supposed but it seems the conditions of the while are never met
while UntitledVoltage(k)>15 && UntitledVoltage(k)<26 && k>=s+n %3rd condition achieves what it was said before.
OpenVoltage(x)=UntitledVoltage(k);
disp (OpenVoltage) *DOESN'T SHOW ANYTHING*
k=k+1;
x=x+1;
end;
OpenVoltage=OpenVoltage(setdiff(1:numel(OpenVoltage),x+1:numel(OpenVoltage)));
MeanVolt(m)=mean(OpenVoltage);
Arc(k)= MeanVolt(m);
t=t+1;
m=m+1;
n=x;
x=1;
end;
I suspect it has something to do with the 3rd condition of the while loop. I just wanted to eliminate the "reset" capability of the "K" once it finish the for loop, so the next voltage value that met the first two conditions is not taken into account again.
Any help will be appreciated!!
Many thanks
Sergio.
0 件のコメント
採用された回答
Daniel Shub
2013 年 7 月 24 日
When k is equal to 2, s is equal to 2 and n is equal to 0. If UntitledVoltage(2) is not between 15 and 26, then you skip the while loop and then n gets set to 1. Then when k is equal to 3, s is equal to 3 and n is equal to 1 so k>=s+n is not true and you skip the while loop no matter what UntitledVoltage(3) is. This repeats until the your for loop ends.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!