0% found this document useful (0 votes)
18 views38 pages

Day 5

The document discusses different types of MATLAB programs including scripts, functions, anonymous functions, local functions, nested functions, and private functions. It provides examples of how to create and call each type of function. Key points include that scripts contain sequential MATLAB commands while functions can accept inputs and return outputs, and functions provide more flexibility than scripts. Local functions and nested functions are only visible within the same file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views38 pages

Day 5

The document discusses different types of MATLAB programs including scripts, functions, anonymous functions, local functions, nested functions, and private functions. It provides examples of how to create and call each type of function. Key points include that scripts contain sequential MATLAB commands while functions can accept inputs and return outputs, and functions provide more flexibility than scripts. Local functions and nested functions are only visible within the same file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Day 5 - MATLAB

Prof. Soma Das


Department of Computer Science and
Engineering
Institute of Engineering & Management, Kolkata
Script file

• The simplest type of MATLAB® program is called


a script.
• A script is a file that contains multiple sequential
lines of MATLAB commands and function calls.
• You can run a script by typing its name at the
command line.
Creating a Script file from
Command window
Creating a Script file from
Command window
Enter some code in the script
and “Run”
Adding comments
Functions in MATLAB

• Functions contain one or more sequential


commands and can accept inputs and return
outputs. To write a program with multiple lines of
code, create a named function in a file.
• Alternatively, if you want to define a one-line
function to pass to another function—for instance,
a mathematical expression to pass to the integral
function—you can create an anonymous function.
Functions in MATLAB

• function [y1,...,yN] = myfun(x1,...,xM) declares a


function named myfun that accepts inputs x1,...,xM
and returns outputs y1,...,yN.
• This declaration statement must be the first
executable line of the function.
• Valid function names begin with an alphabetic
character, and can contain letters, numbers, or
underscores.
Functions in MATLAB

You can save your function:


• In a function file which contains only function
definitions. The name of the file must match the
name of the first function in the file.
• In a script file which contains commands and
function definitions. Functions must be at the end of
the file. Script files cannot have the same name as a
function in the file. Functions are supported in scripts
in R2016b or later.
Functions in MATLAB

Files can include multiple local functions or nested


functions. For readability, use the end keyword to
indicate the end of each function in a file. The end
keyword is required when:
• Any function in the file contains a nested function.
• The function is a local function within a function file,
and any local function in the file uses the end
keyword.
• The function is a local function within a script file.
Function with one output

• First create
a function
file.
Some pointers to write a
function
• There should be no space in the function name.
• File name and function name has to be same.
Calling the function
Function with multiple outputs
Function with multiple outputs
Function in a script file
Function in a script file
Multiple functions in a function
file
• Function avg is a local function. Local functions
are only available to other functions within the
same file.
Multiple functions in a function
file
Function with argument
validation
• Define a function that restricts input to a numeric
vector that contains no Inf or NaN elements.
• In the arguments code block, (1,:) indicates that x
must be a vector. The validation functions,
{mustBeNumeric, mustBeFinite}, restrict the
elements in x to numeric values that are not Inf or
NaN.
Function with argument
validation
Function with argument
validation
Different types of functions in
MATLAB
• Anonymous Functions
• Local Functions
• Nested Functions
• Private Functions
Anonymous Functions

• An anonymous function is a function that is not stored in a program file,


but is associated with a variable whose data type is function_handle.
Anonymous functions can accept multiple inputs and return one output.
They can contain only a single executable statement.
• For example, create a handle to an anonymous function that finds the
square of a number:
sqr = @(x) x.^2;
Variable sqr is a function handle. The @ operator creates the handle, and
the parentheses () immediately after the @ operator include the function
input arguments. This anonymous function accepts a single input x, and
implicitly returns a single output, an array the same size as x that contains
the squared values.
Local functions

• MATLAB® program files can contain code for more


than one function. In a function file, the first function in
the file is called the main function. This function is
visible to functions in other files, or you can call it from
the command line. Additional functions within the file
are called local functions, and they can occur in any
order after the main function. Local functions are only
visible to other functions in the same file. They are
equivalent to subroutines in other programming
languages, and are sometimes called subfunctions.
Example of local function
Nested function

• A nested function is a function that is completely contained


within a parent function. Any function in a program file can
include a nested function. For example, this function named
parent contains a nested function named nestedfx:
Requirements of Nested
function
• Typically, functions do not require an end statement. However, to nest any
function in a program file, all functions in that file must use an end
statement.
• You cannot define a nested function inside any of the MATLAB® program
control statements, such as if/elseif/else, switch/case, for, while, or try/catch.
• You must call a nested function either directly by name (without using
feval), or using a function handle that you created using the @ operator (and
not str2func).
• All of the variables in nested functions or the functions that contain them
must be explicitly defined. That is, you cannot call a function or script that
assigns values to variables unless those variables already exist in the function
workspace.
Private function

• Private functions are useful when you want to limit


the scope of a function. You designate a function as
private by storing it in a subfolder with the name
private. Then, the function is available only to
functions and scripts in the folder immediately
above the private subfolder.
Inline function

• Inline is not recommended. Instead use Anonymous function


• f = inline(expr) constructs an inline function object from the
MATLAB® expression contained in expr. The input argument
to the inline function is automatically determined by
searching expr for an isolated lower case alphabetic character,
other than i or j, that is not part of a word formed from several
alphabetic characters. If no such character exists, x is used. If
the character is not unique, the one closest to x is used. If two
characters are found, the one later in the alphabet is chosen.
Inline function
Inline function

• If inline does not return the desired function


variables or if the function variables are in the
wrong order, you can specify the desired variables
explicitly with the inline argument list
Feval command

• [y1,...,yN] = feval(fun,x1,...,xM) evaluates a


function using its name or its handle, and using the
input arguments x1,...,xM.

• The feval function follows the same scoping and


precedence rules as calling a function handle
directly.
Feval command
Comparison between script file
and function file
• Both scripts and functions allow you to reuse
sequences of commands by storing them in code
files.
• Scripts are the simplest type of code file, since they
store commands exactly as you would type them at
the command line.
• However, functions are more flexible and more
easily extensible.
Comparison between script file
and function file
Comparison between script file
and function file
• To calculate the area of another triangle using the same script,
you could update the values of b and h in the script and rerun it.
Each time you run it, the script stores the result in a variable
named a that is in the base workspace.
• However, instead of manually updating the script each time, you
can make your code more flexible by converting it to a function.
• Replace the statements that assign values to b and h with a
function declaration statement. The declaration includes the
function keyword, the names of input and output arguments, and
the name of the function.
Comparison between script file
and function file

Functions have their own workspace, separate from the base workspace.
Therefore, none of the calls to the function triarea overwrite the value of a in the
base workspace. Instead, the function assigns the results to variables a1, a2, and
a3.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy