|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:
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
char. You can set the relative precedence of user-defined objects with the
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. 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.
For built-in functions:
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.
For nonbuilt-in functions:
@polynom/polynom.m) take precedence over other MATLAB functions. Therefore, if you create an M-file called
polynom.mand put it on your path before the constructor
@polynom/polynom.mversion, 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|