主要内容

Function Precedence Order

这个话题解释了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. 变量

    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. Function or class whose name matches an explicitly imported name

    Theimportfunction 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.

  3. 当前功能中的嵌套功能

  4. Local functions within the current file

  5. Function or class whose name matches a wildcard-based imported name

    当函数名称与基于通配符的导入函数匹配时,MATLAB使用导入的化合物名称,并赋予其优先级,其优先级具有相同名称,除了嵌套和本地功能。

  6. Private functions

    Private功能是命名的子文件夹中的函数privatethat is immediately below the folder of the currently running file.

  7. 对象功能

    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.

  8. Class constructors in @ folders

    MATLAB uses class constructors to create a variety of objects (such astimeseries或者audioplayer),您可以使用面向对象的编程来定义自己的类。例如,如果您创建一个类文件夹@polynomand a constructor function@polynom/polynom.m, the constructor takes precedence over other functions namedpolynom.m路径上的任何地方。

  9. Loaded Simulink®models

  10. 当前文件夹中的功能

  11. 按照外观顺序在路径上的其他地方功能

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 files that are not loaded, with file types in this order:

    1. SLX文件

    2. MDL file

  4. Stateflow®chart with a.sfxextension

  5. App file (.mlapp) created using MATLAB App Designer

  6. Program file with a.mlxextension

  7. p文件(即,一个编码程序文件,每分钟扩大)

  8. Program file with a.mextension

例如,如果MATLAB找到一个.mfile 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.

要确定MATLAB调用特定输入的函数,请包括函数名称和呼叫中的输入whichfunction.

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, seeUpdate Code for R2019b Changes to Function Precedence Order.

  • Identifiers cannot be used for two purposes inside a function

  • 没有明确声明的标识符可能不会被视为变量

  • 变量不能在父函数和嵌套功能之间隐式共享

  • Change in precedence of compound name resolution

  • Anonymous functions can include resolved and unresolved identifiers

行为importfunction 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

See Also

相关话题