How to Use the if Statement in MATLAB

By Jim Sizemore, John Paul Mueller

The simplest decision to make is whether to do something — or not. However, you might need to decide between two alternatives in MATLAB. When a situation is true, you perform one task, but when it’s false, you perform another task. There are still other times when you have multiple alternatives and must choose a course of action based on multiple scenarios using multiple related decisions.

Making a simple decision

Starting simply is always best. The if statement makes it possible to either do something when the condition you provide is true or not do something when the condition you provide is false. The following steps show how to create a function that includes an if statement.

  1. Click the arrow under the New entry on the Home tab of the MATLAB menu and select Function from the list that appears.

    You see the Editor window.

    image0.jpg

  2. Delete output_args.

    The example doesn’t provide an output argument, but it does require an input argument.

  3. Change the function name from Untitled to SimpleIf.

    The primary function name must match the name of the file.

  4. Change input_args to Value.

    The term input_args is used only to tell you that you need to provide input arguments. In this case, the function receives a value from the caller to use in the decision-making process.

  5. Type the following code into the function between the comment and the end keyword.

    if Value > 5
     disp(‘The input value is greater than 5!’);
    end

    This code makes a simple comparison. When the input argument, Value, is greater than 5, the function tells you about it. Otherwise, the function doesn’t provide any output at all.

  6. Click Save.

    You see the Select File for Save As dialog box. Notice that the File Name field has the correct filename entered for you. This is the advantage of changing the function name before you save the file for the first time.

    image1.jpg

  7. Click Save.

    The function file is saved to disk.

  8. Type SimpleIf(6) and press Enter in the Command window.

    You see the following output:

    The input value is greater than 5!
  9. Type SimpleIf(4) and press Enter in the Command window.

    The function doesn’t provide any output. Of course, this is the expected reaction.

Adding an alternative option

Many decisions that people make are choices between two options. For example, you might go to the beach today, or choose to stay home and play dominoes based on whether it is sunny. When the weather is sunny, you go to the beach. MATLAB has a similar structure.

The application chooses between two options based on a condition. The second option is separated from the first by an else clause — the application performs the first task, or else it performs the second. The following steps demonstrate how the else clause works.

  1. In the Editor window, with the SimpleIf.m file selected, click the down arrow under Save and choose Save As from the list that appears.

    You see the Select File for Save As dialog box.

  2. Type IfElse.m in the File Name field and click Save.

    MATLAB saves the example using a new name.

  3. Replace the SimpleIf function name with IfElse.

  4. Add the following code after the disp() function call:

    else
     disp(‘The input value is less than 6!’);

    The function can now respond even when the primary condition isn’t met. When Value is greater than 5, you see one message; otherwise, you see the other message.

  5. Click Save.

    The function file is saved to disk.

  6. Type IfElse(6) and press Enter in the Command window.

    You see the following output:

    The input value is greater than 5!
  7. Type IfElse(4) and press Enter in the Command window.

    You see the following output:

    The input value is less than 6!

    The example demonstrates that you can provide alternative outputs depending on what is happening within the application. Many situations arise in which you must choose an either/or type of condition.

Creating multiple alternative options

Many life decisions require more than two alternatives. For example, you’re faced with a menu at a restaurant and want to choose just one of the many delicious options. Applications can encounter the same situation. A user may select only one of the many options from a menu, as an example. The following steps show one method of choosing between multiple options.

  1. In the Editor window, with the IfElse.m file selected, click the down arrow under Save and choose Save As from the list that appears.

    You see the Select File for Save As dialog box.

  2. Type IfElseIf.m in the File Name field and click Save.

    MATLAB saves the example using a new name.

  3. Replace the IfElse function name with IfElseIf.

  4. Add the following code after the first disp() function call:

    elseif Value == 5
     disp(‘The input value is equal to 5!’);

    At this point, the code provides separate handling for inputs greater than, equal to, and less than 5.

  5. Modify the third disp() function statement to read:

    disp(‘The input value is less than 5!’);

    Many people make the mistake of not modifying everything that needs to be modified by an application change. Because you now have a way of handling inputs equal to five, you must change the message so that it makes sense to the user. Failure to modify statements often leads to odd output messages that serve only to confuse users.

  6. Click Save.

    The function file is saved to disk.

  7. Type IfElseIf(6) and press Enter in the Command window.

    You see the following output:

    The input value is greater than 5!
  8. Type IfElseIf(5) and press Enter in the Command window.

    You see the following output:

    The input value is equal to 5!
  9. Type IfElseIf(4) and press Enter in the Command Window.

    You see the following output:

The input value is less than 5!