Function Precedence Order
调用一个对象时, MATLAB 程序的搜索域为
- the current file
- an optional private subfolder relative to the currently running function
- the current folder
- the MATLAB path
具体的搜索顺序为
- Variables in the Workspace
- Function or class whose name matches an explicitly imported name !tocheck
- The
importfunction allows functions with compound names (names comprised of several parts joined by dots) to be called using only the final part of the compound name - When a function name matches an explicit (non-wildcard) imported function, MATLAB uses the imported compound name and gives it precedence over all other functions with the same name
- The
- Nested functions within the current function
- Local functions within the current file
- Function or class whose name matches a wildcard-based imported name
- When a function name matches a wildcard-based imported function, MATLAB uses the imported compound name and gives it precedence over all other functions with the same name, except for nested and local functions
- Private functions
- Private functions are functions in a subfolder named
privatethat is immediately below the folder of the currently running file.
- Private functions are functions in a subfolder named
- 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
- Class constructors in @ folders
- MATLAB uses class constructors to create a variety of objects (such as
timeseriesoraudioplayer), and you can define your own classes using object-oriented programming - For example, if you create a class folder
@polynomand a constructor function@polynom/polynom.m, the constructor takes precedence over other functions namedpolynom.manywhere on the path
- MATLAB uses class constructors to create a variety of objects (such as
- Loaded Simulink models
- Functions in the current folder
- 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:
- Built-in function
- MEX-function
- Simulink model files that are not loaded, with file types in this order:
- Stateflow chart with a
.sfxextension - App file (
.mlapp) created using MATLAB App Designer - Program file with a
.mlxextension - P-file (that is, an encoded program file with a
.pextension) - Program file with a
.mextension
To determine the function MATLAB calls for a particular input, include the function name and the input in a call to the which function.
Change in Rules For Function Precedence Order
Starting in R2019b, MATLAB changes the rules for name resolution, impacting the precedence order of variables, nested functions, local functions, and external functions. For information about the changes and tips for updating your code, see Update Code for R2019b Changes to Function Precedence Order.
- Identifiers cannot be used for two purposes inside a function
- Identifiers without explicit declarations might not be treated as variables
- Variables cannot be implicitly shared between parent and nested functions
- Change in precedence of compound name resolution
- Anonymous functions can include resolved and unresolved identifiers
The behavior of the import function has changed.
- Change in precedence of wildcard-based imports
- Fully qualified import functions cannot have the same name as nested functions
- Fully qualified imports shadow outer scope definitions of the same name
- Error handling when import not found
- Nested functions inherit import statements from parent functions