If-expression only runs the first expression?

I created a for loop for a 5x5 matrix (it is a struct with data in it). But there are some values of the matrix I don't want to calculate. For example, I don't want to calculate cell (1,4). I also don't want to calculate the cell (3,4), (4,4),...
if ~((i_testen == 4) & (welke_pp == 1)) | ((i_testen == 4) & (welke_pp == 3)) | ((i_testen == 4) & (welke_pp == 4)) | ((i_testen == 4) & (welke_pp == 5)) %i_testen stands for the measurementnumber. welke_pp stand for the subjectnumber.
RASI = data_sts(welke_pp,i_testen).VideoSignals(:, strcmp('RASI', data_sts(welke_pp,i_testen).VideoSignals_headers)); %extract data
XY(2,1) = max(RASI) %maximum of RASI
XY(1,1) = 0; %mimimum is set to zero
Begin_Eind_sts.Begin(i_testen) = abs(XY(2,1)); %store data
Begin_Eind_sts.Eind(i_testen) = abs(XY(1,1));
close all %close all opened figures
else
continue %continue with the previous for-loop
end
The problem is that the program runs perfectly, and even when the values for i_testen = 4 and welke_pp = 1, the program goes to 'else' and continues the for loop. But when the next values for the if-expression comes up (being i_testen = 4 and welke_pp = 3), the program doens't jump to 'else'.

댓글 수: 2

Please use the code formatting button to make your code readable.
Jan
Jan 2014년 12월 21일
You have a "matrix", which is a "struct" and the elements are "cells"? These are contradictions and inconsquence confusing.

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

 채택된 답변

Jan
Jan 2014년 12월 21일
편집: Jan 2014년 12월 21일

0 개 추천

Of course your code does not enter the else part, when i_testen = 4 and welke_pp = 3.
if ~((i_testen == 4) & (welke_pp == 1)) | ...
((i_testen == 4) & (welke_pp == 3)) % shortend
The ~ operator matters the first condition only. So for i_testen = 4 and welke_pp = 3, the condition is true. Perhaps you want additional paranetheses?
if ~( ((i_testen == 4) && (welke_pp == 1)) || ...
((i_testen == 4) && (welke_pp == 3)) || ...
((i_testen == 4) && (welke_pp == 4)) || ...
((i_testen == 4) && (welke_pp == 5)) )
I'm used the && and operators, because the expressions are scalars, but the vector operators & and | are correct also.
Or shorter:
if i_testen ~= 4 || any(welke_pp ~= [1,3,4,5])

추가 답변 (1개)

Star Strider
Star Strider 2014년 12월 20일

1 개 추천

The ‘else’ condition may not be necessary.
See if:
if ~((i_testen == 4) & (welke_pp == 1)) | ((i_testen == 4) & (welke_pp == 3)) | ((i_testen == 4) & (welke_pp == 4)) | ((i_testen == 4) & (welke_pp == 5))
. . . CODE . . .
close all %close all opened figures
end
works. The ‘else’ condition is likely implied.

댓글 수: 6

Sam
Sam 2014년 12월 20일
Thanks for your answer, but it doesn't work either...
Star Strider
Star Strider 2014년 12월 20일
편집: Star Strider 2014년 12월 20일
My pleasure. We’ll keep working on this...
Meanwhile, I would set up a separate test script (call it whiteboard.m or something) to test your logic statements. Run different values for i_testen and welke_pp and see how your statement works, such as in your if statement here, without the rest of the code:
if ~((i_testen == 4) & (welke_pp == 1)) | ((i_testen == 4) & (welke_pp == 3)) | ((i_testen == 4) & (welke_pp == 4)) | ((i_testen == 4) & (welke_pp == 5))
I_TESTEN = i_testen
WELKE_PP = welke_pp
end
Displaying the result with ‘I_TESTEN’ and ‘WELKE_PP’ will tell you what your if statement logic is actually doing. (Since MATLAB is case-sensitive, there is no conflict.)
Note that it is equivalent to:
if ((i_testen ~= 4) & (welke_pp ~= 1)) | ((i_testen == 4) & (welke_pp == 3)) | ((i_testen == 4) & (welke_pp == 4)) | ((i_testen == 4) & (welke_pp == 5))
since the negation only applies to the first test condition.
Sam
Sam 2014년 12월 20일
편집: Sam 2014년 12월 20일
When i enter the values i_testen=4 and welke_pp = 1 (this is the first expression after 'if') Matlab says only gives the name of the script 'whiteboard'.
But when I enter the values i_testen=4 and welke_pp=3 (this is the second expression after 'if') Matlab says 'I_TESTEN = 4' and 'WELKE_PP = 3'.
I don't know why...
(look in attached file)
I was thinking more along these lines for testing your code:
i_testenc = inputdlg('Enter a value for ‘i_testen’: ');
i_testen = str2num(i_testenc{:});
welke_ppc = inputdlg('Enter a value for ‘welke_pp’: ');
welke_pp = str2num(welke_ppc{:});
if ~((i_testen == 4) & (welke_pp == 1)) | ((i_testen == 4) & (welke_pp == 3)) | ((i_testen == 4) & (welke_pp == 4)) | ((i_testen == 4) & (welke_pp == 5))
I_TESTEN = i_testen
WELKE_PP = welke_pp
end
This brings up input dialogue windows so you can test your if loop with individual pairs of numbers for both variables. Enter a number for each in the respective windows, click ‘OK’, and see the result in the Command Window.
You can use nested loops if you like, looping over both variables, but this lets you experiment interactively with specific values for your two variables.
Run it and see if it works to help you isolate the problems.
Sam
Sam 2014년 12월 21일
I've ran your code, if I enter the values behind the if-expression, only the first one (i_testen = 4 and welke_pp = 1) works. The other ones doesn't work.
Star Strider
Star Strider 2014년 12월 21일
편집: Star Strider 2014년 12월 21일
I have no idea what you want to do, so I’m not sure what to suggest. The code I wrote is designed to help you troubleshoot your code. You’ll have to experiment with your if logic to get the result you want. That’s the best I can do just now.
I would start with several large sheets of paper, then diagram on them what I want to do in various situations. (In the days when I did this for my FORTRAN programs on the back of 132-column fanfold line-printer paper, it was called a ‘flow chart’.) Write short programs to test your logic so you’re certain it will work as you want it to. Then write your program.
I’ll help as much as I can, but I can only do so much.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

Sam
2014년 12월 20일

편집:

Jan
2014년 12월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by