If statement working partially

2 次查看(过去 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