Programming Previous page   Next Page

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:

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.

Function Precedence Order

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. MATLAB selects the correct function for a given context by applying the following function precedence rules, in the order given:

  1. Subfunctions
  1. 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.

  1. Private functions
  1. 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.

  1. Class constructor functions
  1. 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.

  1. Overloaded methods
  1. MATLAB calls an overloaded method if it is not masked by a subfunction or private function.

  1. Current directory
  1. A function in the current working directory is selected before one elsewhere on the path.

  1. Elsewhere on path
  1. Finally, a function anywhere else on the path is selected.

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 five 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

  1. Built-in (.bi) file
  2. MEX-files
  3. MDL (Simulink® model) file
  4. P-code file
  5. M-file

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.


Previous page  How MATLAB Determines Which Method to Call Querying Which Method MATLAB Will Call Next page

© 1994-2005 The MathWorks, Inc.