Anonymous functions, sub-functions, private functions, overloaded functions, eval function and feval

Anonymous functions, sub-functions, private functions and other types of functions

Anonymous function:

   Anonymous function has no name, nor is .m file which contains only an expression input and output parameters.

Fxy=@(x,y)x.^y+3*x*y

 

 

 

x, y is the input parameter, Fxy function name is

 

Functions:

In Matlab function writes a plurality .m file. File name and the main function of the same name when the first of which is called the primary function appears function, other function is called subroutine, save, external program can only be called main function.

Features:

  1. Functions can only be called by other functions in the same file
  2. Priority subroutine call by name when calling the function, then call built-in functions.
  3. The main function of the same file, independent subfunctions work area, transfer of information between the various functions implemented by input and output parameters.
  4. Help, lookfor not display information about the child functions

 

function F=ex4_19(n)

A=1;

w=2;

phi = pi / 2;

signal=createsig(A,w,phi);

F=signal.^n;

%%%%%%%%subfunction%%%%%%%%%%

function signal=createsig(A,w,phi)

x = 0: pi / 100: 2 * pi;

signal = A * sin (w * x + fees);

 

Private functions and private directory:

Private function refers to the function file located in the private directory private.

Features:

  1. Private function the same structure and normal function.
  2. Private functions can be called directly M files in the private directory.
  3. Use instructions call the private function, priority second only to Matlab built-in functions and subroutines
  4. Help, lookfor not display information about private functions.

 

For example, located in MATLAB ®  file search path folder, create a name for the folder subfolders private. Do not add private to the path. In the name of the private folder for findme.m file, create a function:

 

function findme

% FINDME  An example of a private function.

 

disp('You found the private function.')

 

In the private folder-level directory folder and create a name for visible.m file.

 

function visible

findme

 

Performed first and then perform the function visible findme

 

 

 

 

Overloaded functions:

   And other programming languages ​​language re-board the overloaded functions the same meaning, different input parameters of the same name, function overloading, in actual use will call the function corresponding to the correct parameters and execution

 

eval function and feval:

  1. Eval function used in conjunction with the text variable, in particular the use of the syntax:

eval(s);

s is a string, eval function can be performed to evaluate the expression or statement text string.

Example: eval_exp1:

Array=1:5;

String='[Array*2;Array/2;2.^Array]';

Output=eval(String)

Results of the:

Output =

 

    2.0000    4.0000    6.0000    8.0000   10.0000

    0.5000    1.0000    1.5000    2.0000    2.5000

2.0000    4.0000    8.0000   16.0000   32.0000

 

The obtained value eval function string

 

Example: eval_exp2:

theta=pi;

eval('Output=exp(sin(theta))');

who

 

Results of the:

 

 

 

The eval function performs a string of statements

 

  1. feval function

[y1,...,yN] = Faevl(fun,x1,...,xM)

The meaning of the instruction execution fun function parameters x1, x1 ............ .xM operation, similar to the function handle

例:feval_exp1.m

Array=1:5;

String='[Array*2;Array/2;2.^Array]';

Outpute=eval(String)     

Outputf = feval (String)% feval only accept the function name, error examples

Results of the:

 

 

 

 

例:feval_exp2.m

fun='round'

x=2.5;

Output=feval(fun,x)

 

Results of the:

 

Y = round  to   each element is rounded to the nearest whole number. In the case of the other, that elements fractional part exactly   when, round function from zero rounding to an integer having a greater amplitude.(X)X0.5

Inline functions: will be removed in a future release  inline. Instead, use the  anonymous function . (And therefore only understand)

inline (expr) according MATLAB expr contained ®  expression construct an inline object. Inline function input parameters is automatically determined by searching expr isolated lowercase characters (i or j excluded), lowercase alphabetic characters that are not part of a word constituted by a plurality of alphabetic characters. If there is no such characters, use x. If the character is not unique, the character closest to x is used. If you find two characters, the characters in the alphabet rearward select.

inline (expr, arg1, arg2, ...) of a configuration arg1, arg2 ... inline function specified input parameters. You can use multi-character symbol name.

inline (expr, n) (where n is a scalar) which is constructed an inline function input parameters x, P1, P2, ... to.

Example: This example creates a simple inline function to square a number

 

 

 

 

Example: This example creates an inline function to represent = 3sin formula F (2x 2 ). Inline function generated may be used  argnames and the  formula function to calculate

 

 

 

 

Inline functions related to four commands:

  1. char (fun) inline function converts an array of characters. This is exactly the same formula (fun).
  2. argNames (fun) returns a character inline objects fun cellular array input parameter name in the form of a vector.
  3. Formula (fun) Returns the fun of formula inline objects.
  4. Will vectorize on (fun) of formula in fun. Interposed ^, *, or / 'before any. The result is a vectorized version of the inline function.

 

Priority Function: [LZ1] 

This topic describes MATLAB ®  how to determine the function to be called when the current scope of multiple functions with the same name. The current scope includes the current file, relative to the optional private subfolder of the current folder to run the function, and MATLAB current folder path.

MATLAB using the following order of priority:

  1. variable

Before determined that the name and function match, MATLAB will first have to check the names of the variables in the current workspace.

note:

If you create a function with the same name as a variable, MATLAB can not run the function, until you clear the variable from memory.

  1. Function imported package

Package function associated with a particular folder. Using  import a function package import function, which takes precedence over all other functions of the same name.

  1. Current nested function within the function
  2. Local functions in the current file
  3. Private functions

Private function is the name for the  private subfolders of the folder (that is currently running file folder where the file being below the folder) function.

  1. Object Functions

Object function accepts an object of a particular class to a list of input parameters. If there are a plurality of objects with the same name, class MATLAB check input parameters to determine the function to be used.

  1. @ Folder class constructor

MATLAB uses the class constructor to create a variety of objects (such as  timeseries or  audioplayer), you can also use object-oriented programming defined in its own class. For example, if you create a class folder  @polynom and constructors  @polynom/polynom.m, the constructor take precedence over any location named in the path of  polynom.m other functions.

  1. Loaded Simulink ®  models
  2. The current folder function
  3. Function of the other path position, display order

Determining the priority function in the same file folder, MATLAB regardless of file type in the following order:

  1. Built-in functions
  2. MEX function
  3. Unloaded Simulink model files, file types in the following order:
    1. SLX file
    2. MDL file
    3. Created using MATLAB App Design Tool App file ( .mlapp)
    4. With  .mlx the extension of the program file
    5. P file (i.e., with the  .p coding program file extension)
    6. With  .m the extension of the program file

For example, if MATLAB to find the same name in the same folder as  .m the file and P file, it uses the P file. Because P file does not automatically regenerate, so make sure you rebuild the file while editing P file.

To determine the function of MATLAB called for specific input, please to  which include the function name and enter the call function.

 


 [lz1] function execution order, some functions not covered by the knowledge annotation focus met easy to find when the function execution order-related issues

Guess you like

Origin www.cnblogs.com/asahiLikka/p/11577643.html