Documentation Center

  • Trial Software
  • Product Updates

Function Precedence Order

This topic explains how MATLAB® determines which function to call when multiple functions in the current scope have the same name. The current scope includes the current file, an optional private subfolder relative to the currently running function, the current folder, and the MATLAB path.

MATLAB uses this precedence order:

  1. Variables

    Before assuming that a name matches a function, MATLAB checks for a variable with that name in the current workspace.

      Note:   If you create a variable with the same name as a function, MATLAB cannot run that function until you clear the variable from memory.

  2. Imported package functions

    A package function is associated with a particular folder. When you import a package function using the import function, it has precedence over all other functions with the same name.

  3. Nested functions within the current function

  4. Local functions within the current file

  5. Private functions

    Private functions are functions in a subfolder named private that is immediately below the folder of the currently running file.

  6. Object functions

    An object function accepts a particular class of object in its input argument list. When there are multiple object functions with the same name, MATLAB checks the classes of the input arguments to determine which function to use.

  7. Class constructors in @ folders

    MATLAB uses class constructors to create a variety of objects (such as timeseries or audioplayer), and you can define your own classes using object-oriented programming. For example, if you create a class folder @polynom and a constructor function @polynom/polynom.m, the constructor takes precedence over other functions named polynom.m anywhere on the path.

  8. Functions in the current folder

  9. Functions elsewhere on the path, in order of appearance

When determining the precedence of functions within the same folder, MATLAB considers the file type, in this order:

  1. Built-in function

  2. MEX-function

  3. Simulink® model, with file types in this order:

    1. SLX file

    2. MDL file

  4. P-file (that is, an encoded program file with a .p extension)

  5. Program file with a .m extension

For example, if MATLAB finds a .m file and a P-file with the same name in the same folder, it uses the P-file. Because P-files are not automatically regenerated, make sure that you regenerate the P-file whenever you edit the program file.

To determine the function MATLAB calls for a particular input, include the function name and the input in a call to the which function. For example, determine the location of the max function that MATLAB calls for double and int8 values:

testval = 10;
which max(testval)
% double method
built-in (matlabroot\toolbox\matlab\datafun\@double\max)
testval = int8(10);
which max(testval)
% int8 method
built-in (matlabroot\toolbox\matlab\datafun\@int8\max)

For more information, see:

Was this topic helpful?