MATLAB Programming
MATLAB Programming MATLAB Programming
5 Types of Functions count = 125 225 325 425 s.counter1(25) count = 150 250 350 450 Now increment the second counter: s.counter2(25) count = -75 -175 -275 -375 s.counter2(25) count = -50 -150 -250 -350 Go back to the first counter and you can see that it keeps its own value for count: s.counter1(25) count = 175 275 375 475 Restrictions on Assigning to Variables The scoping rules for nested, and in some cases anonymous, functions require that all variables used within the function be present in the text of the M-file code. Adding variables to the workspace of this type of function at run time is not allowed. MATLAB issues an error if you attempt to dynamically add a variable to the workspace of an anonymous function, a nested function, or a function that contains a nested function. Examples of operations that might use dynamic assignment in this way are shown in the table below. 5-26
Nested Functions Type of Operation Evaluating an expression using eval or evalin, or assigning a variable with assignin Loading variables from a MAT-file with the load function Assigning to a variable in a MATLAB script Assigning to a variable in the MATLAB debugger How to Avoid Using Dynamic Assignment As a general suggestion, it is best to avoid using the eval, evalin, andassignin functions altogether. Use the form of load that returns a MATLAB structure. Convert the script to a function, where argument- and result-passing can often clarify the code as well. You can declare the variable to be global. For example, to create a variable X for temporary use in debugging, use K>> global X; X = value One way to avoid this error in the other cases is to pre-declare the variable in the desired function. Examples of Nested Functions This section shows a few examples of how you can use nested functions. These examples are intended to show you how to program with this type of function. For more mathematically oriented examples, see the MATLAB Mathematics documentation. Theexamplesinthissectioninclude • “Example 1 — Creating a Function Handle for a Nested Function” on page 5-27 • “Example 2 — Function-Generating Functions” on page 5-29 Example 1 — Creating a Function Handle for a Nested Function The following example constructs a function handle for a nested function and then passes the handle to the MATLAB fplot function to plot the parabola 5-27
- 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 402 and 403: 5 Types of Functions Overview of MA
- 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 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
- Page 452 and 453: 6 Data Import and Export File to im
- Page 454 and 455: 6 Data Import and Export Example of
- Page 456 and 457: 6 Data Import and Export was select
- Page 458 and 459: 6 Data Import and Export Import fro
- Page 460 and 461: 6 Data Import and Export File Forma
- Page 462 and 463: 6 Data Import and Export or save ju
- Page 464 and 465: 6 Data Import and Export Data Compr
- Page 466 and 467: 6 Data Import and Export Preference
- Page 468 and 469: 6 Data Import and Export Saving fro
- Page 470 and 471: 6 Data Import and Export Loading Bi
- Page 472 and 473: 6 Data Import and Export Accessing
- Page 474 and 475: 6 Data Import and Export Sharing Me
5 Types of Functions<br />
count =<br />
125 225 325 425<br />
s.counter1(25)<br />
count =<br />
150 250 350 450<br />
Now increment the second counter:<br />
s.counter2(25)<br />
count =<br />
-75 -175 -275 -375<br />
s.counter2(25)<br />
count =<br />
-50 -150 -250 -350<br />
Go back to the first counter and you can see that it keeps its own value for<br />
count:<br />
s.counter1(25)<br />
count =<br />
175 275 375 475<br />
Restrictions on Assigning to Variables<br />
The scoping rules for nested, and in some cases anonymous, functions require<br />
that all variables used within the function be present in the text of the M-file<br />
code. Adding variables to the workspace of this type of function at run time is<br />
not allowed.<br />
<strong>MATLAB</strong> issues an error if you attempt to dynamically add a variable to the<br />
workspace of an anonymous function, a nested function, or a function that<br />
contains a nested function. Examples of operations that might use dynamic<br />
assignment in this way are shown in the table below.<br />
5-26