This article is a beginner’s guide about the if-else structure in MATLAB. In this article, we’ll see:
- The if-else structure in MATLAB
- How to write “if A is not equal to B”
- How to write “if A is less than or equal to B”
- How to use logical operators in an if-else structure (MATLAB or/and operators)
- The switch case structure in MATLAB
The If-Else Structure in MATLAB
You can use a conditional statement (i.e. if-else structure) in MATLAB when you want to execute instructions based on a statement that is true or false. The basic syntax to do so is the following:
if firstCondition firstInstructions elseif secondCondition secondInstructions else otherInstructions end
The condition following the “if” keyword is a Boolean value (i.e. true or false). You can define this condition right after the “if” or as a Boolean variable using MATLAB operators. We will see how to use the “if” statement with logical operators in the following sections.
If A is Not Equal to B
A And B Are Matrices or Vectors
You can write A and B as matrices or vectors and compare them to each other in order to run the instructions based on the outcome. Here’s how to do that:
if ~isequal(A, B) instruction end
For example, if you define A and B as vectors, you have the following:
if ~isequal([3 5], [4 5]) b = 6; end
In MATLAB, we get
Note that it works the same way if you use matrices for A and B.
A And B Are Numbers
If A and B are numbers, you can use the symbol “~=.” Here is the structure for defining an if statement using that symbol:
if A~=B instruction end
Here is an example with actual numbers:
if 3~=4 a = 5; end
In MATLAB, we get
Note that the isequal MATLAB command also works when A and B are numbers even, though we usually use this method for vectors and matrices.
A And B Are Strings
If you want to use the condition “A is not equal to B” with A and B defined as strings, then you need to use the MATLAB function strcmp. This function returns a logical value that is true if the two input arguments of the function are the same and false otherwise.
if ~strcmp(A, B) c = 7; end
Here is a standard example of how to use the “if” statement when A and B are strings:
if ~strcmp('X', 'Y') c = 7; end
In MATLAB, we get
If A is Less Than or Equal to B
You can also compare numbers and execute code based on the outcome of that comparison. Here’s the standard structure to execute the code when A is less than or equal to B:
if A<=B instruction end
Here’s an example with actual numbers:
if 3<=4 d = 8; end
In MATLAB, we get
MATLAB Boolean: Using Logical Operators
If you want to execute code based on more complex operations, then you will have to define variables that will be true or false based on logical operations on Boolean values. For this, you need logical operators such as the MATLAB or/and.
MATLAB AND Operator
Let’s say that we want to execute instructions based on the conditional statement (A is not equal to B) AND (C is equal to D). Here’s the basic structure to do so:
if (A~=B)&&(C==D) instruction end
MATLAB OR Operator
Now, let’s tackle an example with the conditional statement (A is not equal to B) OR (C is equal to D):
if (A~=B)||(C==D) instruction end
MATLAB AND/OR For Vectors
Note that & and | can be used instead of && and || If you want to operate with vectors. The output when using & and | will be vectors of Boolean values. For example, if you are using the vectors [0 1] and [1 0], here’s what you’ll have :
However, if you try to use || and && to compare vectors, you will get an error in MATLAB. To my knowledge, there is no way to use a vector in front of the if statement, therefore if you’re writing a statement that will be used in an if/else structure you will always use && and ||.
Switch Case In MATLAB
The basic switch case syntax used for multiple case statements in MATLAB allows you to run instructions based on different values of a variable. You can use this kind of structure for numbers as well as string values. Here’s the basic structure for a switch case in MATLAB:
switch variable case firstCaseValue firstCaseInstructions case secondCaseValue secondCaseInstructions otherwise otherInstructions end
It works as follows:
- Use the switch keyword followed by the variable that will be compared to the variables that are in front of the case keyword.
- When the variable in front of the switch keyword has the value of the variable in front of the case keyword, then the corresponding instructions will be executed.
- If the value of the variable in front of the switch keyword doesn’t match any of the variables in front of the case keyword, then the instruction following the otherwise keyword will be executed.
Here’s an example of a switch case that will display “yes” if the value of “variable” is the letter “y,” “no” if the value of “variable” is the letter “n,” and “wrong value” if “variable” has another value.
switch variable case 'y' disp('Yes!'); case 'n' disp('No!'); otherwise disp('Wrong value!'); end
You can also add values to one case as follows:
switch variable case {'y', 'yes'} disp('Yes!'); case {'n', 'no'} disp('No!'); otherwise disp('Wrong value!'); end
This will display “yes” if the value of “variable” is either ‘y’ or ‘yes’ and “no” if it is ‘n’ or ‘no’.
Key takeaways:
- The basic if else MATLAB structure is:
if firstCondition firstInstructions elseif secondCondition secondInstructions else otherInstructions end
- If you want to write the conditional statement “if A is not equal to B,” you can use the following structure:
- The conditional statement “A is less than or equal to B” is:
if A<=B instruction end
- Use && and || for the and/or logical operator in if-else MATLAB structures.
- Use the following switch case structure for numbers and strings:
switch variable case firstCaseValue firstCaseInstructions case secondCaseValue secondCaseInstructions otherwise otherInstructions end
If you want to learn more about the tools that helped me stop wasting time doing mindless work (such as generating Excel reports, Word documents, or creating clean and simple user interfaces) I wrote a small reference book about it: