Skip to content Skip to navigation

Connexions

You are here: Home » Content » Programming with M-Files: If Statements

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of a lens

      Lenses

      A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

      What is in a lens?

      Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

      Who can create a lens?

      Any individual Connexions member, a community, or a respected organization.

    • External bookmarks
  • E-mail the author

Recently Viewed

Programming with M-Files: If Statements

Module by: Darryl Morrell

Summary: This is a tutorial on using if statements in m-file scripts.

The If Statement

The if statement is one way to make the sequence of computations executed by in an m-file script depend on variable values. The if statement has several different forms. The simplest form is


if expression
    % Commands to execute if expression is true
end
where expression is a logical expression that is either true or false. (Information about logical expressions is available in Programming with M-Files: Logical Expressions.) For example, the following if statement will print "v is negative" if the variable v is in fact negative:

if v < 0
    disp('v is negative')
end

A more complicated form of the if statement is


if expression
    % Commands to execute if expression is true
else
    % Commands to execute if expression is false
end
For example, the following if statement will print "v is negative" if the variable v is negative and "v is not negative" if v is not negative:

if v < 0
    disp('v is negative')
else
    disp('v is not negative')
end

The most general form of the if statement is


if expression1
    % Commands to execute if expression1 is true
elseif expression2
    % Commands to execute if expression2 is true
elseif expression3
    % Commands to execute if expression3 is true
    ...
else
    % Commands to execute if all expressions are false
end
The following if statement is an example of this most general statement:

if v < 0
    disp('v is negative')
elseif v > 0
    disp('v is positive')
else
    disp('v is zero')
end

Note that in all of the examples in this module, the commands inside the if statement are indented relative to the if, else, elseif, and end statements. This is not required, but is common practice and makes the code much more readable.

Comments, questions, feedback, criticisms?

Send feedback