Chapter 10
Attaway MATLAB 4E
Advanced Functions
Chapter 10 Attaway MATLAB 4E Variable # of Arguments So far in the - - PowerPoint PPT Presentation
Advanced Functions Chapter 10 Attaway MATLAB 4E Variable # of Arguments So far in the functions that we ve written, there has been a fixed number of input arguments and a fixed number of output arguments It is possible to have a
Attaway MATLAB 4E
Advanced Functions
So far in the functions that weve written, there has been a fixed
number of input arguments and a fixed number of output arguments
It is possible to have a variable number of arguments, both
input arguments and output arguments
A built-in cell array varargin can be used to store a variable
number of input arguments
a built-in cell array varargout can be used to store a variable
number of output arguments
These are cell arrays because the arguments could be different
types, and cell arrays can store different kinds of values in the different elements.
The cell array varargin stores a variable number of
This could be used to store all of the input arguments to
the function, or only some of them
The function nargin returns the total number of input
Since varargin is a cell array, use curly braces to refer
Two kinds of function headers
All input arguments stored in varargin:
function outarg = fnname(varargin)
varargin stores some of the input arguments but not all:
function outarg = fnname(input args, varargin)
For example, if coordinates of a point are being passed
function outarg = fnname(varargin)
In this case, x is stored in varargin{1}, y is stored in {2},
and if z is passed, it is in varargin{3} function outarg = fnname(x,y,varargin)
In this case, x and y are stored in input arguments x and
y, and if z is passed, it is in varargin{3}
Note: in both cases, nargin will be 3
The cell array varargout stores a variable number of output
arguments
As with input arguments, some output arguments can be built in
if they are always going to be returned, or varargout can store all
function varargout = fnname(input args) function [output args, varargout] = fnname(input args)
Since varargout is a cell array, use curly braces to refer to the
elements, which are the output arguments
To call this function:
[variables] = fnname(input args);
[x, y, z] = fnname(…
the value of nargout would be 3
A nested functions is when an outer function has within it inner
function(s)
When functions are nested, every function must have an end
statement (much like loops)
The general format of a nested function is as follows:
body of outer function inner function header body of inner function end % inner function more body of outer function end % outer function
The inner function can be in any part of the body of the outer function
so there may be parts of the body of the outer function before and after the inner function
Anonymous functions are really, really simple, short
General form:
fnhanvar = @ (input arguments) functionbody
The input arguments and function body are similar to
the @ returns the handle of the function, which is a
the variable on the left of the assignment stores the
Calling the function is accomplished by using the
fnhanvar(input arguments)
An advantage of anonymous functions is that you
However, it is useful to store groups of related
Here is an example of an anonymous function that
>> cirarea = @ (radius) pi * radius .^ 2;
Examples of calling it:
>> cirarea(4) ans = 50.2655 >> areas = cirarea(1:4) areas = 3.1416 12.5664 28.2743 50.2655
If there arent any input arguments, you still have to
>> prtran = @ () fprintf('%.2f\n',rand); >> prtran() 0.95 >> prtran prtran = @ () fprintf('%.2f\n',rand)
Function handles can be created for all functions, not
fnhanvar = @fnname
where fnname can be the name of a built-in or user-
This can then be used to call the function:
fnhanvar(input arguments)
instead of
fnname(input arguments)
So, why do we need that?
For example, the following function function receives
>> type fnfn function out = fnfn(x,fnhan)
end
Examples of calling this function will be shown on the
First, create function handles
>> type myfn % Here is a user-defined function function outy = myfn(x)
end >> fnha = @myfn; % Function handle for a user-defined function >> fnhb = @(x) x .^ 2 - 3; % Function handle for an anonymous function >> fnhc = @cos; % Function handle for a built-in function
Next, call the function by passing a vector and function handle:
>> x = 3:.5:6; >> y = fnfn(x, fnha) y = 20.0000 33.8750 53.0000 78.1250 110.0000 149.3750 197.0000
>> y = fnfn(x, fnhb) y = 6.0000 9.2500 13.0000 17.2500 22.0000 27.2500 33.0000 >> fnfn(x,fnhc) ans =
>> fnfn(x,@sum) ans = 31.5000
The function str2func receives a string which is a
The function func2str converts a function handle to a
built-in function function fplot receives a function
built-in function function feval receives a function
Function timeit can be used to time functions; it is
One argument is passed which is the handle of the
It returns the time in seconds
Recursion is when something is defined in terms of
Recursive functions are functions that call themselves
There has to be a way to stop this, otherwise, infinite
recursion will occur
Sometimes either iteration or recursion can be used to
An iterative definition for the factorial of an integer n
n! = 1 * 2 * 3 * ... * n
A recursive definition is:
n! = n * (n - 1)! general case 1! = 1 base case
With a recursive definition, there is always a general
A function that implements the recursive definition has an
if-else statement to choose between the general and base cases:
function facn = fact(n) % fact recursively finds n! % Format: fact(n) if n == 1 facn = 1; else facn = n * fact(n-1); end end
Trying to pass just the name of a function to a function
Thinking that nargin is the number of elements in
Forgetting the base case for a recursive function
Use anonymous functions whenever the function body
Store related anonymous functions together in one
If some inputs and/or outputs will always be passed
Use iteration instead of recursion when possible.