# Wrong code. Does not get into the while loop.

1 view (last 30 days)
Sergio on 24 Jul 2013
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:
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.

Daniel Shub on 24 Jul 2013
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.
##### 2 CommentsShowHide 1 older comment
Sergio on 24 Jul 2013
I solved it puting as a condition of the while K>=p when P = K at the end of the while.