Open In App

Function Precedence Order in MATLAB

Last Updated : 04 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we shall discuss the function precedence order in MATLAB. The first thing to keep in mind is that the order explained below is defined for an individual scope (current workspace); it does not hold when there are multiple workspaces in play.
Now, let us explore the order individually. 

Variables  Functions

In a case where the user defines a variable with the same name as a function, MATLAB will never call the function until the variable is deleted from memory. See the example below.

Matlab




% function
function geeks(x)
    disp(x)
end


The above is a function that prints the passed parameter. Now, when we declare a variable with same name in current workspace, and then call the function, it will throw an error, saying that there is a variable and function with same name, kindly clear the variable it was unintentional. 

Output:

 

Thus, a variable gets precedence over a function with the same name.

Imported Functions

After the variable names, when a locally defined function matches the name of an explicitly imported function, MATLAB will always use the imported function for the given name. 

In the following example, we create a dummy function of the name numericalJacobian. And then import the following function

Matlab




import curvefit.numericalJacobian


Now, when we call the same function in the command window, instead of calling the local function, MATLAB will use the imported one.

Output:

 

As it can be seen in the error message, the used function is curvefit.numericalJacobian and not the local numericalJacobian. 

Nested Functions

After imported functions, precedence is given to the nested functions within the parent function i.e, in a function, the nested functions are given precedence in call order. In other words, a nested function is called before its parent function.

Local Functions

After nested functions, MATLAB gives precedence to local functions declared in a file. Thus, so far, we have the following order. 

Variables > Imported functions > Nested functions > Local functions (not nested)

Private Functions

After the locally defined functions, the precedence comes to private functions. A private function in MATLAB is a function that is stored in a subfolder named private, to limit the scope of that particular function. We shall not discuss the creation of private functions as it is not in this article’s scope.

Object Functions

The next precedence is given to an object function, which is a function that takes a specified class of objects as input arguments.  Whenever, there are multiple object functions with same name, MATLAB gives precedence according to the list of input class objects.

Class Constructors 

Whenever, the name of function matches that of a class constructor function, the precedence will always be given to the class constructor, irrespective of the user defined function’s precedence.

Simulink Models

In many MATLAB applications, there is a required to load some SIMULINK models, such as signal processing. In case there is a conflict with the name of local functions and a loaded SIMULINK model’s function, the precedence will be given to local function.

Functions in different paths

Lastly, MATLAB gives precedence to functions defined in the current folder over any other function defined elsewhere on path.

Precedence of functions in same folder

When deciding the precedence order of functions in same folder, MATLAB decides it based on the file type and the order followed is:

  • Built-in function
  • MEX function
  • In case of a copied SIMULINK model function file. following sub-order is taken
    • SLX extension
    • MDL extension
  • Functions with .sfx extension
  • MATLAB App-designer file – .mlapp extension
  • File with .mlx extension
  • An encoded program file – .p extension
  • Ordinary MATLAB script .m type

Conclusions 

In this article, we vastly discussed the function precedence order in MATLAB and used code examples, whenever required.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads