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
Data structures: The Data structures are variables which store more than one value. In order to made sense to store more than one value in a variable, the values must in some

Algorithm for the function e: The algorithm for the function eoption is as shown: Use the menu function to show the 4 choices. Error-check (an error would take place

Use of built-in colormaps: MATLAB has built-in colormaps, it is also possible to generate others by using combinations of any colors. For illustration, the following generates

True color matrice: The true color matrices are the other way to represent images. The true color matrices are 3-dimensional matrices. The first two coordinates are the coordi

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 products by for loop: an illustration, when 5 is passed to be the value of the input argument n, the function will compute and return 1 + 2 + 3 + 4 + 5, or 15: >> s

Execution steps: Whenever the program is executed, the steps below will take place: The script calcandprintarea starts executing. The calcandprintarea calls the readr


Creating a cell array: The other method of creating a cell array is easy to assign values to particular array elements and build it up element by element. Though, as explained

str2num function - String: The function str2num does the opposite; it takes the string in which a number is stored and converts it to the type double: >> num = str2num('123.