MATLAB Lesson 8 - Logic and Control

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.

Calculate the square root y of the variable x only when the value of x is non-negative.

The command y = sqrt(x) is only executed if x >=0 is true.

The variable y is not defined when x >=0 is false.

>>  if x >= 0
                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

Give MATLAB code to calculate y where y = -1 when x < 0 and y = 2 when x > 2.

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.

>>  if x < 0
                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 value of f(x) is -2x when x < 0; x(x-2) when x is in [0, 2] and log(x-1) otherwise. Calculate f(x).

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.

>>  if x < 0
                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.

Open a new file plantsim.m in the MATLAB editor.

You could also click on New M-file, the first icon in the MATLAB toolbar, or use the menus: File -> New -> Blank M-file.

>>  edit plantsim.m

Using the MATLAB editor has several advantages. These include:

 

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.