MATLAB Programming
MATLAB Programming MATLAB Programming
5 Types of Functions Overview of MATLAB Function Types There are essentially two ways to create a new function in MATLAB: in a command entered at run-time, or in a file saved to permanent storage. The command-oriented function, called an anonymous function, isrelatively brief in its content. It consists of a single MATLAB statement that can interact with multiple input and output arguments. The benefit of using anonymous functions is that you do not have to edit and maintain a file for functions that require only a brief definition. There are several types of functions that are stored in files (called M-files). The most basic of these are primary functions and subfunctions. Primary functions are visible to other functions outside of their M-file, while subfunctions, generally speaking, are not. That is, you can call a primary function from an anonymous function or from a function defined in a separate M-file, whereas you can call a subfunction only from functions within the same M-file. (See the Description section of the function_handle reference page for information on making a subfunction externally visible.) Two specific types of primary M-file functions are the private and overloaded function. Private functions are visible only to a limited group of other functions. This type of function can be useful if you want to limit access to a function, or when you choose not to expose the implementation of a function. Overloaded functions act the same way as overloaded functions in most computer languages. You can create multiple implementations of a function so that each responds accordingly to different types of inputs. ThelasttypeofMATLABfunctionisthenested function. Nested functions are not an independent function type; they exist within the body of one of the other types of functions discussed here (with the exception of anonymous functions), and also within other nested functions. One type of function that is not discussed in this chapter is the MATLAB built-in function. Built-ins are defined only as executables internal to MATLAB. See “Built-In Functions” on page 3-111 for more information. 5-2
Anonymous Functions Anonymous Functions Anonymous functions give you a quick means of creating simple functions without having to create M-files each time. You can construct an anonymous function either at the MATLAB command line or in any M-file function or script. This section covers • “Constructing an Anonymous Function” on page 5-3 • “Arrays of Anonymous Functions” on page 5-6 • “Outputs from Anonymous Functions” on page 5-7 • “Variables Used in the Expression” on page 5-8 • “Examples of Anonymous Functions” on page 5-11 Constructing an Anonymous Function The syntax for creating an anonymous function from an expression is fhandle = @(arglist) expr Starting from the right of this syntax statement, the term expr represents the body of the function: the code that performs the main task your function is to accomplish. This consists of any single, valid MATLAB expression. Next is arglist, which is a comma-separated list of input arguments to be passed to the function. These two components are similar to the body and argument list components of any function. Leading off the entire right side of this statement is an @ sign. The @ sign is the MATLAB operator that constructs a function handle. Creating a function handle for an anonymous function gives you a means of invoking the function. It is also useful when you want to pass your anonymous function in a call to some other function. The @ sign is a required part of an anonymous function definition. 5-3
- Page 352 and 353: 4 M-File Programming Simple Functio
- Page 354 and 355: 4 M-File Programming 3 Type inmem t
- Page 356 and 357: 4 M-File Programming sqr = @(x) x.^
- Page 358 and 359: 4 M-File Programming Call plotFhand
- Page 360 and 361: 4 M-File Programming This false res
- Page 362 and 363: 4 M-File Programming Create two equ
- Page 364 and 365: 4 M-File Programming • Overloadin
- Page 366 and 367: 4 M-File Programming Here is a more
- Page 368 and 369: 4 M-File Programming axis([xmin fix
- Page 370 and 371: 4 M-File Programming This section c
- Page 372 and 373: 4 M-File Programming addOptional(na
- Page 374 and 375: 4 M-File Programming % Display the
- Page 376 and 377: 4 M-File Programming Arguments That
- Page 378 and 379: 4 M-File Programming Enabling Case-
- Page 380 and 381: 4 M-File Programming Property Funct
- Page 382 and 383: 4 M-File Programming Using nargin a
- Page 384 and 385: 4 M-File Programming 4-52
- Page 386 and 387: 4 M-File Programming Determining Wh
- Page 388 and 389: 4 M-File Programming 7 Function els
- Page 390 and 391: 4 M-File Programming While the comm
- Page 392 and 393: 4 M-File Programming strcmp str1 st
- Page 394 and 395: 4 M-File Programming On the other h
- Page 396 and 397: 4 M-File Programming City Temp. Hea
- Page 398 and 399: 4 M-File Programming function [vout
- Page 400 and 401: 4 M-File Programming Running Extern
- Page 404 and 405: 5 Types of Functions Note Function
- Page 406 and 407: 5 Types of Functions Arrays of Anon
- Page 408 and 409: 5 Types of Functions Display the da
- Page 410 and 411: 5 Types of Functions If you change
- Page 412 and 413: 5 Types of Functions • “Example
- Page 414 and 415: 5 Types of Functions Supply the val
- Page 416 and 417: 5 Types of Functions Nested Functio
- Page 418 and 419: 5 Types of Functions • From a fun
- Page 420 and 421: 5 Types of Functions not shared bet
- Page 422 and 423: 5 Types of Functions You can now us
- Page 424 and 425: 5 Types of Functions Using a Subfun
- Page 426 and 427: 5 Types of Functions count = 125 22
- Page 428 and 429: 5 Types of Functions shape. The mak
- Page 430 and 431: 5 Types of Functions % FILTFCN. Eac
- Page 432 and 433: 5 Types of Functions 0.6667 filt2(1
- Page 434 and 435: 5 Types of Functions unless you dec
- Page 436 and 437: 5 Types of Functions Accessing Help
- Page 438 and 439: 5 Types of Functions 5-38
- Page 440 and 441: 6 Data Import and Export Working wi
- Page 442 and 443: 6 Data Import and Export • “Dat
- Page 444 and 445: 6 Data Import and Export file that
- Page 446 and 447: 6 Data Import and Export Function r
- Page 448 and 449: 6 Data Import and Export Importing
- Page 450 and 451: 6 Data Import and Export • rowhea
5 Types of Functions<br />
Overview of <strong>MATLAB</strong> Function Types<br />
There are essentially two ways to create a new function in <strong>MATLAB</strong>: in a<br />
command entered at run-time, or in a file saved to permanent storage.<br />
The command-oriented function, called an anonymous function, isrelatively<br />
brief in its content. It consists of a single <strong>MATLAB</strong> statement that can<br />
interact with multiple input and output arguments. The benefit of using<br />
anonymous functions is that you do not have to edit and maintain a file for<br />
functions that require only a brief definition.<br />
There are several types of functions that are stored in files (called M-files).<br />
The most basic of these are primary functions and subfunctions. Primary<br />
functions are visible to other functions outside of their M-file, while<br />
subfunctions, generally speaking, are not. That is, you can call a primary<br />
function from an anonymous function or from a function defined in a separate<br />
M-file, whereas you can call a subfunction only from functions within the<br />
same M-file. (See the Description section of the function_handle reference<br />
page for information on making a subfunction externally visible.)<br />
Two specific types of primary M-file functions are the private and overloaded<br />
function. Private functions are visible only to a limited group of other<br />
functions. This type of function can be useful if you want to limit access to a<br />
function, or when you choose not to expose the implementation of a function.<br />
Overloaded functions act the same way as overloaded functions in most<br />
computer languages. You can create multiple implementations of a function<br />
so that each responds accordingly to different types of inputs.<br />
Thelasttypeof<strong>MATLAB</strong>functionisthenested function. Nested functions<br />
are not an independent function type; they exist within the body of one of the<br />
other types of functions discussed here (with the exception of anonymous<br />
functions), and also within other nested functions.<br />
One type of function that is not discussed in this chapter is the <strong>MATLAB</strong><br />
built-in function. Built-ins are defined only as executables internal to<br />
<strong>MATLAB</strong>. See “Built-In Functions” on page 3-111 for more information.<br />
5-2