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()



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 =

@ (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
Technique to creating this structure: An alternative technique of creating this structure, that is not as efficient, includes using the dot operator to refer to fields in the

Binary Search: The binary search supposes that the vector has been sorted first. The algorithm is just similar to the way it works whenever looking for a name in a phone direc

Function cirarea - Anonymous functions: The function handle name is cirarea. The one argument is passed to the input argument radius. The body of the function is an expression

Evaluating a string: The function eval is used to compute a string as a function. For illustration, below is the string 'plot(x)'is interpreted to be a call to plot the functi

Illustration sorting vectors of structures: This function sorts the structures depend only on the price field. A more common function is shown next, that receives a string whi

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

A matlab function to calculate filter order

Dot Product: The dot or inner product of two vectors a and b is written as a • b and is defined as  In another words, this is like matrix multiplication when multiplyi

Splits a string : The strtok function splits a string into pieces; it can be called in many ways. The function receives one string as an input argument. It appears for the fir

Illustration of Matrix solutions: For illustration, consider the three equations below with 3unknowns x 1 ,x 2 , and x 3 : We can write this in the form Ax = b here A