Programming and Data Types    

How MATLAB Determines Which Method to Call

In MATLAB, functions exist in directories in the computer's file system. A directory may contain many functions (M-files). Function names are unique only within a single directory (e.g., more than one directory may contain a function called pie3). When you type a function name on the command line, MATLAB must search all the directories it is aware of to determine which function to call. This list of directories is called the MATLAB path.

When looking for a function, MATLAB searches the directories in the order they are listed in the path, and calls the first function whose name matches the name of the specified function.

If you write an M-file called pie3.m and put it in a directory that is searched before the specgraph directory that contains the MATLAB pie3 function, then MATLAB uses your pie3 function instead (note that this is not true for built-in functions like plot, which are always found first).

Object-oriented programming allows you to have many methods (MATLAB functions located in class directories) with the same name and enables MATLAB to determine which method to use based on the type or class of the variables passed to the function. For example, if p is a portfolio object, then

calls @portfolio/pie3.m because the argument is a portfolio object.


  Specifying Precedence of User-Defined Classes Selecting a Method