MATLAB - Function Precedence Order



Function precedence order, in simple words, is the set of rules that MATLAB follows to decide which function or variable to use when there are multiple options with the same name. It's like a hierarchy that MATLAB uses to determine which one to pick.

MATLAB follows this precedence order −

Variables

If a name is recognized as a variable in the current workspace, MATLAB will use it as a variable.

Example

x = 5;
y = x + 2;

In this example, x is a variable, and MATLAB recognizes it as such.

Function or Class with Explicit Import

If a function or class name matches an explicitly imported name, it takes precedence.

Example

import MyPackage.myFunction;
result = myFunction();

Here, myFunction from MyPackage is explicitly imported and used.

Nested Functions

Functions within the current function take priority.

Example

function outer()
   x = 10;
   function inner()
      x = 5;
   end
end

In this case, the x inside inner has precedence over the x in outer.

Local Functions

Functions within the current file have precedence.

Consider the following script myScript.m

Example

function result = myScript()
   x = 3;
   y = localFunction();
end

function z = localFunction()
   z = 7;
end

In this example, localFunction in the same file is used.

Function or Class with Wildcard-Based Import

If a function matches a wildcard-based import, it takes precedence, except for nested and local functions.

Example

import MyPackage.*;  % Imports all functions in MyPackage
result = myFunction();

Here, if myFunction is imported via a wildcard, it has precedence.

Private Functions

Private functions in a subfolder named "private" take precedence.

In MATLAB, private functions are those functions that are specifically designed for use within a single MATLAB script or function, and they are typically stored in a subfolder named "private" within the same folder as the script that uses them.

% In the folder containing your script
/private/myPrivateFunction.m

When you have a script in the same folder as this "private" subfolder, any functions located in the "private" subfolder are given precedence over functions with the same name located elsewhere.

In the example above, if you have a script in the same folder as the "private" subfolder containing myPrivateFunction.m, MATLAB will prioritize the use of myPrivateFunction within that script, making it a convenient way to organize and manage script-specific functions.

Object Functions

Object functions are determined by the class of input arguments.

object = MyClass();
result = object.myMethod();

The myMethod called on an object of MyClass invokes that specific object function.

MATLAB determines which object function to use based on the class of the input arguments. When you call a function on an object, like object.myMethod(), MATLAB will automatically invoke the method that is associated with the class of the object.

This makes sure that the object's behavior is executed, allowing you to interact with and change objects for the class and characteristics.

In the example above, result = object.myMethod();, myMethod is a function that operates on objects of the class MyClass, and MATLAB uses it accordingly when the object on that class is created.

Class Constructors

Constructors in class folders take precedence when creating objects.

myObj = MyClass();

Here, MyClass constructor from the @MyClass folder is used.

In object-oriented programming within MATLAB, a class constructor is a special function used to create instances of a class, which are known as objects. When you create an object using a class constructor, MATLAB will prioritize the constructor located in the class folder over other functions with the same name.

This ensures that objects of that class are properly initialized and can be customized during their creation. In your example, myObj = MyClass();, MATLAB specifically uses the constructor function located in the @MyClass folder to initialize the myObj object, setting it up for use with the defined class properties and methods.

Loaded Simulink Models

If Simulink models are loaded, they have precedence.

In the context of Simulink, if you have multiple Simulink models open or loaded in your MATLAB session, these models are given priority over other functions or variables when you're working on Simulink-related tasks. This ensures that your Simulink environment operates seamlessly by using the components and settings within the currently loaded models, allowing for efficient modeling and simulation without interference from unrelated functions or variables. It helps maintain the integrity and consistency of your Simulink-based work.

Functions in the Current Folder

Functions in the same folder as your script are prioritized.

This means that if you have functions stored in the same folder as your script or MATLAB file, these local functions take precedence. MATLAB will choose functions from the current folder over functions with the same name located elsewhere on the MATLAB path. This is helpful for ensuring your code uses specific local functions tailored to your project's needs.

Functions Elsewhere on the Path

Functions on the MATLAB path are evaluated in the order they appear.

When MATLAB can't find a function in the current folder or through other specific precedence rules, it looks at the functions placed on its search path. Functions located on the MATLAB path are considered, and they are evaluated in the order they appear on the path. This provides a way to access and use functions that are not directly in the current folder, making it convenient for handling libraries and commonly used functions in various projects.

Advertisements