Programming and Data Types |

**Selecting a Method**

When you call a method for which there are multiple versions with the same name, MATLAB determines the method to call by:

- Looking at the classes of the objects in the argument list to determine which argument has the highest object precedence; the class of this object controls the method selection and is called the
*dispatch type*. - Applying the
*function precedence order*to determine which of possibly several implementations of a method to call. This order is determined by the location and type of function.

**Determining the Dispatch Type**

MATLAB first determines which argument controls the method selection. The class type of this argument then determines the class in which MATLAB searches for the method. The controlling argument is either:

User-defined objects take precedence over the MATLAB built-in classes such as `double`

or `char`

. You can set the relative precedence of user-defined objects with the `inferiorto`

and `superiorto`

functions, as described in Object Precedence.

MATLAB searches for functions by name. When you call a function, MATLAB knows the name, number of arguments, and the type of each argument. MATLAB uses the dispatch type to choose among multiple functions of the same name, but does not consider the number of arguments.

The function precedence order determines the precedence of one function over another based on the type of function and its location on the MATLAB path. From the perspective of method selection, MATLAB contains two types of functions: those built into MATLAB, and those written as M-files. MATLAB treats these types differently when determining the function precedence order.

MATLAB selects the correct function for a given context by applying the following function precedence rules, in the order given.

- If there is a method in the class directory of the dispatching argument that has the same name as a MATLAB built-in function, then this method is called instead of the built-in function.

MATLAB built-in functions take precedence over both subfunctions and private functions. Therefore, subfunctions or private functions with the same name as MATLAB built-in functions can never be called.

- Subfunctions take precedence over all other M-file functions and overloaded methods that are on the path and have the same name. Even if the function is called with an argument of type matching that of an overloaded method, MATLAB uses the subfunction and ignores the overloaded method.

- Private functions are called if there is no subfunction of the same name within the current scope. As with subfunctions, even if the function is called with an argument of type matching that of an overloaded method, MATLAB uses the private function and ignores the overloaded method.

- Constructor functions (functions having names that are the same as the @ directory, for example
`@polynom/polynom.m`

) take precedence over other MATLAB functions. Therefore, if you create an M-file called`polynom.m`

and put it on your path before the constructor`@polynom/polynom.m`

version, MATLAB will always call the constructor version.

**Selecting Methods from Multiple Directories**

There may be a number of directories on the path that contain methods with the same name. MATLAB stops searching when it finds the first implementation of the method on the path, regardless of the implementation type (MEX-file, P-code, M-file).

**Selecting Methods from Multiple Implementation Types**

There are four file precedence types. MATLAB uses file precedence to select between identically named functions in the same directory. The order of precedence for file types is:

For example, if MATLAB finds a P-code and an M-file version of a method in a class directory, then the P-code version is used. It is, therefore, important to regenerate the P-code version whenever you edit the M-file.

How MATLAB Determines Which Method to Call | Querying Which Method MATLAB Will Call |