“CHAPTER
Control
5” flow
and
Operators
11:1
1PM
5.1 MATLAB is also a
programming language. Like
other computer programming
languages,
MATLAB has some decision
making structures for control of
command execution. These
decision making or control of
structures include for loops,
while loops, and if-else-end
constructions. Control of Back to Agenda Page
5.2 Control flow
MATLAB has four control flow
structures: the if statement, the
for loop, the while loop,
and the switch statement.
The simplest form of
5.3 The ``if...end'' structure the if statement is
if expression
MATLAB supports the variants of
statements
\if" construct. end
• if ... end
• if ... else ... end
• if ... elseif ... else ... end Back to Agenda Page
Here are some examples
based on the familiar
quadratic formula.
1. discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is
negative, roots are
imaginary');
• end
2. discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is
negative, roots are
imaginary');
else
disp('Roots are real, but may be
repeated')
end
3. discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is
negative, roots are
imaginary');
elseif discr == 0
disp('Discriminant is zero, roots
are repeated')
else
disp('Roots are real')
end
It should be noted that:
• elseif has no space between
else and if (one word)
• no semicolon (;) is needed
at the end of lines containing
if, else, end
• indentation of if block is not
required, but facilitate the
reading.
• the end statement is
required
Table 5.1: Relational and
Operator logical
/ Description
operators
> Greater than
< Less than
>= Greater than orNote
equal
that the “equal to"
to relational operator
<= Less than or equal toof two equal
consists
== Equal to signs (==)(with no space
»= Not equal to between them), since =
& AND operator is reserved for the
j OR operator assignment operator.
» NOT operator
5.2.3 The ``for...end'' loop
In the for ... end loop, the execution of a
command is repeated at afixed and
predeter-mined number of times. The
syntax is
for variable = expression
statements
end
Usually, expression is a vector of the
form i:s:j. A simple example of for loop is
for ii=1:5
x=ii*ii
end
Back to Agenda Page
The ``while...end'' loop
This loop is used when the number of
passes is not specified. The looping
continues until a
stated condition is satisfied. The while
loop has the form:
while expression
statements
end
The statements are executed as long as
expression is true.
x=1
while x <= 10 Back to Agenda Page
5.2.5 Other flow structures
• The break statement. A while loop can be
terminated with the break statement, which
passes control to the first statement after
the corresponding end. The breakstatement
can also be used to exit a for loop.
• The continue statement can also be used to
exit a for loop to pass immediately to the
next iteration of the loop, skipping the
remaining statements in the loop.
• Other control statements include return,
continue, switch, etc. For more detailabout
Back to Agenda Page
5.2.6 Operator precedence
We can build expressions that use any
combination of arithmetic, relational, and
logical operators. Precedence rules determine
the order in which MATLAB evaluates an
expression.
Here we add other operators in the list. The
precedence rules for MATLAB are shown in this
list (Table 5.2), ordered from highest (1) to
lowest (9) precedence level. Operators Operator precedence
We can build expressions that use any combination of arithmetic, relational, and logical
operators. Precedence rules determine the order in which MATLAB evaluates an expression.
are evaluated from left to right.
Table 5.2:
Operator
Precedence
precedence
Operator
1 Parentheses ()
2 Transpose (: 0), power (.^),
matrix power (^)
3 Unary plus (+), unary minus (¡),
logical negation (»)
4 Multiplication (: ¤), right division (: =),
left division (:n),
matrix multiplication (¤), matrix
right division (=),
matrix left division (n)
5 Addition (+), subtraction
(¡)
6 Colon operator (:)
7 Less than (<), less than or equal
5.3 Saving output to a file In addition to
displaying output on the screen, the
command fprint can be used for writing the
output to a file. The saved data can
subsequently be used by MATLAB or other
softwares. To save the results of some
computation to a file in a text format
requires the following steps:
1. Open a file using fopen
tion Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section Head
Section Header
Chapter 6
Debugging
M-files
tion Header • Section Header • Section Header • Section Header • Section Header • Section Header • Section Head
Section Header
6.1 Introduction
This section introduces general techniques
for finding errors in M-files. Debugging is the
process by which you isolate and fix errors in
your program or code.
Debugging helps to correct two kind of
errors:
• Syntax errors - For example omitting a
parenthesis or misspelling a function
name.
• Run-time errors - Run-time errors are
Back to Agenda Page
6.2 Debugging process
We can debug the M-files using the
Editor/Debugger as well as using debugging
functions from the Command Window. The
debugging process consists of
• Examining values
• Preparing for debugging
• Correcting problem
• Setting breakpoints
• Ending debugging
• Running an M-file with breakpoints
• Stepping through an M-file
Back to Agenda Page
6.2.1 Preparing for debugging
Here we use the Editor/Debugger for
debugging. Do the following to prepare for
debugging:
• Open the file
• Save changes
• Be sure the ¯le you run and any ¯les it
calls are in the directories that are on
thesearch path. Back to Agenda Page
6.2.2 Setting breakpoints
Set breakpoints to pause execution of the
function, so we can examine where the
problem might be. There are three basic
types of breakpoints:
• A standard breakpoint, which stops at a
specified line.
• A conditional breakpoint, which stops at a
specified line and under specified
6.2.3 Running with breakpoints
After setting breakpoints, run the M-¯le from
the Editor/Debugger or from the Command
Window. Running the M-¯le results in the
following:
² The prompt in the Command Window
changes to K>> indicating that MATLAB is in
debug mode.
² The program pauses at the ¯rst breakpoint.
6.2.4 Examining values
While the program is paused, we can view
the value of any variable currently in
theworkspace. Examine values when we
want to see whether a line of code has
producedthe expected result or not. If the
result is as expected, step to the next line,
and continue running. If the result is not as
expected, then that line, or the previous
line, contains an Back to Agenda Page
6.2.5 Correcting and ending debugging
While debugging, we can change the value
of a variable to see if the new value
produces expected results. While the
program is paused, assign a new value to
the variable in the Com-mand Window,
Workspace browser, or Array Editor. Then
continue running and stepping
through the program.
Back to Agenda Page
6.2.6 Ending debugging
After identifying a problem, end the
debugging session. It is best to quit debug
mode before editing an M-file. Otherwise,
you can get unexpected results when you
run the file. To end
debugging, select Exit Debug Mode from the
Debug menu.
Back to Agenda Page
6.2.7 Correcting an M-file
To correct errors in an M-file,
• Quit debugging
• Do not make changes to an M-file while
MATLAB is in debug mode
• Make changes to the M-file
• Save the M-file
• Clear breakpoints
• Run the M-file again to be sure it
produces the expected results. For
details on debugging process, seeBack to Agenda Page
6.2.3 Running with breakpoints
After setting breakpoints, run the M-file from
the Editor/Debugger or from the Command
Window. Running the M-file results in the
following:
• The prompt in the Command Window
changes to K>> indicating that MATLAB is
in debug mode.
• The program pauses at the first breakpoint.
Thank you!
Insert a parting or call-to-action message
here.