If statement working partially

조회 수: 3 (최근 30일)
Gonzalo Guerrero
Gonzalo Guerrero 2022년 6월 21일
댓글: Voss 2022년 6월 22일
Hi,
I have been havin problems with the following if statment. My data loads in a loop, which will run depending on the if statment. When it comes to the elseif everything run very smoothly, however, when it comes to the if conditions, it doesn't. I tried to put a keyboard afterwards, because the Time variable gave me error, although once the keyboard is placed, I can put the whole if statment and it will work. As soon as i press continue, it doesn't.
Could you help, please? :D
Thank you
while n<=length(DigMark.codes)
if n==length(DigMark.codes)+1
break;
end
while nn<=4
if n==length(DigMark.codes)+1
break;
end
if nn==4
n=n+1;
nn=1;
continue;
end
if DigMark.codes (n) == 65 || DigMark.codes(n)== 67 || DigMark.codes(n)== 68 ||...
DigMark.codes(n)== 69 && DigMark.codes(n+1)~= 88
Times = find(EMG{nn}.times > DigMark.times(n)-.0004& EMG{nn}.times < DigMark.times(n)+.0004);
Times=Times(1);
Time=EMG{nn}.times(Times);
Code= DigMark.codes(n);
ValuesEMG=(EMG{nn}.values(Times:Times+316));
CalibrateEMG=(0-mean(EMG{nn}.values(Times-316:Times)));
ValuesEMG=(ValuesEMG+CalibrateEMG);
P2PEMG= max(ValuesEMG(48:179))-min(ValuesEMG(48:179));
RMSEMG=rms(EMG{nn}.values(Times-316:Times));
.
.
.
nn=n+1;
elseif DigMark.codes(n)==66 || DigMark.codes(n)== 70 || DigMark.codes(n)== 71 && DigMark.codes(n+1)~= 88
times = find(EMG{nn}.times > DigMark.times(n)-.0004& EMG{nn}.times < DigMark.times(n)+.0004);
times=times(1);
Time=EMG{nn}.times(times);
Code= DigMark.codes(n);
ValuesEMG=(EMG{nn}.values(times:times+161));
CalibrateEMG=(0-mean(EMG{nn}.values(times-314:times)));
ValuesEMG=(ValuesEMG+CalibrateEMG);
P2PEMG= max(ValuesEMG(16:end))-min(ValuesEMG(16:end));
RMSEMG=rms(EMG{nn}.values(times-316:times));
.
.
.
nn=nn+1;
else
n=n+1;
nn=1;
end
end
end
  댓글 수: 1
Stephen23
Stephen23 2022년 6월 22일
Replace
x == 65 || x == 67 || x == 68 || x == 69 && y ~= 88
with
ismember(x,[65,67:69]) && y ~= 88

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

답변 (1개)

Voss
Voss 2022년 6월 21일
Part of the problem might be the usage of || and &&.
In MATLAB, && takes precedence over ||, so an expression like this:
x == 65 || x == 67 || x == 68 || x == 69 && y ~= 88
is interpreted as this:
x == 65 || x == 67 || x == 68 || (x == 69 && y ~= 88)
but I think you probably mean this:
(x == 65 || x == 67 || x == 68 || x == 69) && y ~= 88
Here's a concrete case where it makes a difference:
x = 65;
y = 88;
disp( x == 65 || x == 67 || x == 68 || x == 69 && y ~= 88 )
1
disp( (x == 65 || x == 67 || x == 68 || x == 69) && y ~= 88 )
0
Another potential problem is that you let n go up to length(DigMark.codes) but then try to access DigMark.codes(n+1).
  댓글 수: 2
Gonzalo Guerrero
Gonzalo Guerrero 2022년 6월 22일
Thanks for your answer!
I considered that too, but It is weird that the line of elseif works, but then the if doesn't...
I have changed the while loop for a for loop instead and now it works perfectly. It might be just the loop wasn't the right one, for some reason I don't know :S
Voss
Voss 2022년 6월 22일
I don't think either line (if or elseif) was working as you probably intended. Maybe the elseif appeared to work for whatever values it happened to get.

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

카테고리

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