Conditional (if) statements in Matlab

Article posted on Sunday, 11th, August, 2013 at 12:34 am

Conditional statements are simple lines of code which will execute other code if certain criteria are met. Sound confusing? It’s easier than it sounds…

In Matlab, you can write lines of code that execute if, and only if a certain criteria has been met. These criteria come in the form of comparisons between variables, where the comparisons can only return true or false (1 or 0). For example, if I want to check someone is old enough to drink alcohol, where a given age is stored in a variable, we would write:

age = 27;
age > 18

On its own, running this code would return 1. That is because the second line is true. If the code evaluated to false, it would return 0. We haven’t put this in an if statement yet, so it’s not particularly useful…

if age > 18
  disp("Hello, what can I get you to drink?")

That’s all there is to it! When the conditional statement evaluates true, the code within the code block will execute. Otherwise, the code will be ignored.

So what else can you check in an if statement? Well, you first need to get acquainted with the relational operators. These are symbols which are used to compare variables with each other. Each one is explained below, comparing variable A to variable B:

A < B    % A is less than B
A > B    % A is greater than B
A <= B   % A is less than or equal to B
A >= B   % A is greater than or equal to B
A == B   % A is equal to B
A ~= B   % A is not equal to B

Note that you cannot directly compare strings using direct comparison. Fortunately Matlab has your back, and you can use the strcmp function which returns true if the two imput string variables are the same. For example:

my_name = input("What is your name?", "s")

if strcmp(name, "Alex")
  disp("You are one cool dude!")

This will compare the name that is stored in the my_name variable and compare it to the hard coded string, ‘Alex’. If the statement returns true (i.e. my_name = ‘Alex’), then it will display the message. If you were feeling malicious, and wanted to complement everyone that wans’t called Alex, you’d use the Matlab ‘not’ symbol before the function call to strcmp:

if ~strcmp(name, "Alex")

That’s pretty much all you need to know about how to set conditional statements – but what if you want to do something in the event that the if statement does not execute? Well, as you’ve seen, you can write another if statement underneath specifying the opposite condition (e.g. if name is Alex, then if name isn’t Alex), but that would just be plain crazy, when we have th else clause at our disposal!

my_name = input("What is your name?", "s")

if strcmp(name, "Alex")
  disp("You are one cool dude!")
  disp("I feel sorry for you")

Now we have code which is guaranteed to execute something even if the conditional statement is not true. This can be used for all sorts of things, like validation of input, refusing to process variables that will break your code, and much much more besides! Go out and try it… NOW!

Tags: , , , , , ,

Join in the Conversation

comments powered by Disqus