Programming |
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.
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:
@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 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
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 |
© 1994-2005 The MathWorks, Inc.