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

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

i want to run 4 instances of my matlab code on 4 processor cores. im executing the job from head node. i created a parallel job and assigned number of workers. but i don''t get bac

I have a frequency response data. How do I convert that to state space? I am given a 6 row and 3 column data (steady state). How do i convert that to state space model?

IS Functions for Strings: There are many functions for strings, that return logical true or false. The function isletter returns the logical true when the character is a lette

Reduced Row Echelon Form: The Gauss Jordan technique results in a diagonal form; for illustration, for a 3 × 3 system: The Reduced Row Echelon Forms take this one step

For Loops which do not use an iterator Variable in the action: In all the illustrations that we seen so far, the value of the loop variable has been used in same way in the ac

Solving 2 × 2 systems of equations: However this may be easy in a MATLAB, in normal finding solutions to the systems of equations is not. The systems which are 2 × 2 are, thou

num2str function: The num2str function, that converts real numbers, can be called in many ways. If only the real number is passed to the num2str function, it will generate a s

ischar function: The ischar function return the logical true if an array is a character array, or logical false if not. >> vec = 'EK127'; >> ischar(vec) ans =