Lesson 5: Selection: Akos Ledeczi and Mike Fitzpatrick
Lesson 5: Selection: Akos Ledeczi and Mike Fitzpatrick
Lesson 5: Selection: Akos Ledeczi and Mike Fitzpatrick
by
x == 2
block
control statement
control
statement
x == 2
block
control statement
control
statement
x == 2
block
Executing a different set of statements
based on the condition:
function guess_my_number(x)
if x == 2
fprintf('Congrats! You guessed my number!\n');
else
fprintf('Not right, but a good guess.\n');
end
control statement
x == 2
block2
block1
control statement
x == 2
block2
block1
control statement
x == 2
block2
block1
if-statement:
if conditional
block
end
Produces a result that depends on
the relation between its two operands
It can appear outside if-statements!
Logical values:
◦ Non-zero: true
◦ Zero: false
◦ MATLAB returns 1 for true
How to combine logical values?
Logical operators:
not:
◦ flips the value of its (single) operand
and:
◦ true if and only if both of its operands are true
or:
◦ false if and only if both of its operands are false
function ultimate_question(x)
if x == 42
fprintf('Wow! You answered the
question.\n'); elseif x < 42
fprintf('Too small. Try again.\n');
else
fprintf('Too big. Try again.\n');
end
Here is a version with nesting:
function ultimate_question_nested(x)
if x == 42
fprintf('Wow! You answered the question.\n');
else
if x < 42
fprintf('Too small. Try again.\n');
else
fprintf('Too big. Try again.\n');
end
end
Here is another version with nesting:
function ultimate_question_nested2(x)
if x <= 42
if x == 42
fprintf('Wow! You answered the question.\n');
else
fprintf('Too small. Try again.\n');
end
else
fprintf('Too big. Try again.\n');
end
Functions that behave differently based on
◦ Number of input or output arguments
◦ Type of input or output arguments
Many built-in functions are
polymorphic (sqrt, max, size, plot, etc.)
How do we make our
functions polymorphic?
Two built-in functions:
◦ nargin: returns the number of actual input
arguments that the function was called with
◦ nargout: returns the number of output
arguments that the function caller requested
function [table summa] = multable(n, m)
if nargin < 2
m = n;
end
if nargout == 2
summa = sum(table(:));
end
A function declaration specifies:
◦ Name of the function,
◦ Number of input arguments, and
◦ Number of output arguments
Function code and documentation specify:
◦ What the function does, and
◦ The type of the arguments
◦ What the arguments represent
Robustness
◦ A function is robust if it handles erroneous input and
output arguments, and
◦ Provides a meaningful error message
function [table summa] = multable(n, m)
if nargin < 1
error('must have at least one input argument');
end
if nargin < 2
m = n;
elseif ~isscalar(m) || m < 1 || m ~= fix(m)
error('m needs to be a positive integer');
end
if ~isscalar(n) || n < 1 || n ~= fix(n)
error('n needs to be a positive integer');
end
if nargout == 2
summa = sum(table(:));
end
Extra text that is not part of the code
MATLAB disregards it
if nargin < 1
error('must have at least one input argument');
end
…
>> help multable
Variables:
◦ Local
◦ Global
◦ Persistent
Persistent variable:
◦ It’s a local variable, but its value persists from one
call of the function to the next.
◦ Relatively rarely used
◦ None of the bad side effects of global variables.