If-statement
Sometimes it is necessary to have some code that is only executed when a condition is satisfied. In MATLAB this is done using an if statement.
The simplest form is
if logical_expression
    % Code here is only executed if logical_expression is true
    % otherwise execution continues after the end statement
end
Anything after a % is a comment, so is ignored by MATLAB.
The command y = sqrt(x) is only executed if x >=0 is true.
The variable y is not defined when x >=0 is false.
                y = sqrt(x)
        end
There may be more than one condition.
if logical_expression1
    % Code here is only executed if logical_expression1 is true
    % otherwise execution continues after the end statement
elseif logical_expression2
    % Code here is only executed if logical_expression1 is false
    % and logical_expression2 is true
end
The statement y = -1 is executed only if the condition x<0 is true.
The statement y = 2 is executed only if the condition x<0 is false and the condition x>2 is true.
The variable y is not defined when both conditions are false, that is, when 0 <= x <= 2.
                y = -1
        elseif x > 2
                y = 2
        end
Default code may also be specified.
if logical_expression1
    % Code here is only executed if logical_expression1 is true
    % otherwise execution continues after the end statement
elseif logical_expression2
    % Code here is only executed if logical_expression1 is false
    % and logical_expression2 is true
else
    % Code here is only executed if none of the logical expressions are true
end
The first block is executed when x < 0 is true.
The elseif is only reached if x < 0 is false, that is x >= 0, so you only need to check x <=2.
The else is reached when all previous conditions are false, that is x > 2.
                f = -2*x
        elseif x <= 2
                f = x*(x-2)
        else
                f = log(x-1)
        end
Script files and layout
You can enter simple commands directly in the MATLAB command window. However for more complicated programs it is much better to use a MATLAB script file. This is just a plain text file with an extension .m. To execute the commands in a script M-file you simply type the name of the file (without the .m extension) in the command window.
You could also click on New M-file, the first icon in the MATLAB toolbar, or use the menus: File -> New -> Blank M-file.
Using the MATLAB editor has several advantages. These include:
- Syntax highlighting:
- Comments: Anything after a % character is a comment, so is ignored by MATLAB. These are coloured green in the MATLAB editor.
- Keywords: MATLAB keywords such as if, elseif, else, end, for are coloured blue.
- Layout: It is good programming practice to indent conditional statements
and loops to make the structure of your code as clear as possible. This is very simple to
achieve using the MATLAB editor using the following two steps:
- Edit menu then Select All item or Ctrl+a shortcut.
- Text menu then Smart Indent item or Ctrl+i shortcut.
- Breakpoints: The ability to stop execution of your MATLAB code at any line and examine the values of variables, or continue execution line by line. This is most useful when debugging complex programs.
Self-test Exercise
Write a MATLAB if statement to calculate y where y = 1 if x > pi/2, y = sin(x) if x is in [0, pi/2] and y = 0 otherwise.Answer:
if x > pi/2
y = 1
elseif x >= 0
y = sin(x)
else
y = 0
end
Use the mouse to select the text between the word "Answer" and here to see the answer.
Summary
MATLAB uses if paired with a matching end to provide conditional execution of statements. Additional elseif statements and/or an else statement may be used.
It is good programming practice to create M-files with a clear structure and plenty of comments. This can easily be done using the MATLAB editor.