MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!