Illustration of anonymous functions, MATLAB in Engineering

Illustration of anonymous functions:

Dissimilar functions stored in the M-files, when no argument is passed to an anonymous function, the parentheses should still be in the function definition and in the function call. For illustration, here is an anonymous function which prints an arbitrary real number with two decimal positions, and also a call to this function:

 

>> prtran = @ () fprintf('%.2f\n',rand);

>> prtran()

0.95

 

Just typing the name of the function handle will show its contents, that is the function definition.

>> prtran

prtran =

@ () fprintf('%.2f\n',rand)

 

This is why to call a function, even though no arguments are passed, the parentheses should be used.

To save the anonymous function, it can be saved to a MAT-file, and then it can be loaded when required.

 

>> cirarea = @ (radius) pi * radius .^2;

>> save anonfns cirarea

>> clear

>> load anonfns

>> who

Your variables are:

cirarea

>> cirarea

cirarea =

@ (radius) pi * radius .^2

 

The other anonymous functions can be appended to this MAT-file. However, a benefit of anonymous functions is that they do not have to be saved in separate M-files; it is often helpful to save groups of associated anonymous functions in a MAT-file. If there are several anonymous functions which are often used, they can be saved in a MAT-file and then loaded from this MAT-file in every MATLAB Command Window.

Posted Date: 10/20/2012 6:46:53 AM | Location : United States







Related Discussions:- Illustration of anonymous functions, Assignment Help, Ask Question on Illustration of anonymous functions, Get Answer, Expert's Help, Illustration of anonymous functions Discussions

Write discussion on Illustration of anonymous functions
Your posts are moderated
Related Questions
function numden: The function numden will return individually the numerator & denominator of a symbolic expression: >> sym(1/3 + 1/2) ans = 5/6 >> [n, d] =

Illustration of if - else statement: The one application of an if-else statement is to check for errors in the inputs to a script. For illustration, a former script prompted t

Passing arguments to functions: In all these functions examples faraway, at least one of the arguments was passed in the function call to be the value(s) of the equivalent inp


Vector operations: As vectors are special cases of matrices, the matrix operations elaborated (addition, subtraction, multiplication, scalar multiplication, transpose) work on

Example of Exponential function modular program: In order to view the distinction in the approximate value for e as n increases, the user kept choosing Limit & entering larger

Logical scalar values: The MATLAB also has or and and operators which work element wise for the matrices: These operators will compare any of the two vectors or matric

Use of While loop: Here is an illustration of calling the function, passing 5000 for the value of the input argument high. >> factgthigh(5000) ans = 5040 The itera

Finding a sting - function strfind: The function strfind does necessarily similar thing, except that the order of the arguments does make dissimilarity. The common form is str

Example of image processing: The other illustration generates a 5 × 5 matrix of arbitrary integers in the range from 1 to the number of colors; the resultant image is as shown