Biomedical Engineering Reference
In-Depth Information
block is executed. The expression is always evaluated,
and control flow always follows with the statement after
the end.
The valued to be returned by the m-file is 1 if the
argument x is less than zero. The mathematical statement
above is easily translated into the MATLAB statement
if x < 0f ¼ 1;
2.1b.3.2.2 If-then-else
The if.then statement provides the ability to pro-
gram one type of conditional execution control flow. The
if.then.else construct provides the ability to
implement a control flow where one of two blocks, but
not both, are executed.
In MATLAB, the syntax of the if.then.else
statement is
where the variable f is the variable whose value is
returned by the function.
If x is less than zero then the value to be returned has
been determined (f ¼ 1) and no more MATLAB state-
ments are evaluated. If the value of x is not less than zero,
then f ¼ 1 is not executed and the final value of f must
still be determined.
If the value of x is not less than zero, then either it is
equal to zero or it is greater than zero. According to the
mathematical definition above, if x is equal to zero, then
the value of f should be 0; If x is greater than zero, then
the value of f should be 1.
The MATLAB script that implements this function
will have three expression-statement pairs. However,
the three statements should not be executed as a se-
quence; if the first assignment statement is executed,
then the evaluation of f should be stopped. Can you
tell why?
To execute only those statements that should be ex-
ecuted and no others, use the structure to test multiple
conditions and evaluate only the statements that satisfy
the conditions. The control structure to implement this
design requires the elseif clause in the conditional
statement. The final code is:
if expression
statement1
else
statement2
end
The expression is always evaluated; if the expression is
true, then the block statement1 is executed; if the
expression is evaluated to be false, then the block
statement2 is executed.
There is another variant of conditional execution in
MATLAB, the if.elseif.else.end statement
with the syntax
if expression1
statement1
elseif expression2
statement2
else
statement3
end
if x < 0
f ¼ -1;
elseif x ¼¼ 0
f ¼ 0;
elseif x > 0
f ¼ 1;
end
As a followup to this problem, we leave it to the reader to
show that this MATLAB code performs the same func-
tion as the built-in function sign.
The elseif clause allows for the evaluation of
an alternate expression; if the first expression is false
and the second expression is true, then the block
statement2 is executed. Here is an example:
Example 2.1b.2 Simple control flow using if.then.
else
2.1b.3.2.3 Switch statement
In most block-structured programming languages, in-
cluding MATLAB, there is a facility for conditionally ex-
ecuting a sequence of statements froman arbitrary number
of alternatives. Of course, one could implement this
control flow structure using a large if.elseif.end
structure, but there is a more convenient shorthand
notation in MATLAB, the switch.case.
otherwise.endstructure. The syntaxof theMATLAB
switch statement is
Write a MATLAB script to compute the following
function:
8
<
1 x < 0
0 x ¼ 0
1
fðxÞ¼
:
x > 0
Solution
In this example you are asked to create an m-file,
a MATLAB script that computes the function f defined
above. The value returned by the script is one of three
values that depend on the input. The mathematics can be
easily translated into MATLAB commands.
switch switch_expr
case case_expr
statement,.,statement
Search WWH ::




Custom Search