Matlab

How to Use Nested Functions in MATLAB

Nested functions in MATLAB can define functions within other functions. This can be useful for organizing code, making code more reusable, and improving performance.

A nested function is a function that is created inside another function in MATLAB. The special thing about nested functions is that they can use and change variables that are defined in the parent function.

Syntax

These nested functions in MATLAB can easily access parent function variables. Here’s the syntax for defining nested functions in MATLAB:

function parent

disp('Parent function')

nestedfx

function nestedfx

disp('Nested function')

end

end

Example Code

Below we have given a MATLAB code of a nested function:

function parentFunction

x = 10;

 

nestedFunction1()

 

% Nested function 1

function nestedFunction1

disp('Inside nestedFunction1');

disp(x); % Accessing the variable x from the parent function

y = 20;

 

nestedFunction2()

 

% Nested function 2

function nestedFunction2

disp('Inside nestedFunction2');

disp(x); % Accessing the variable x from the parent and nested function 1

disp(y); % Accessing the variable y from nested function 1

end

end

end

Above, MATLAB defines the main function name as parentFunction, and it defines two nested functions: nestedFunction1 and nestedFunction2.

The nestedFunction1 displays the value of the variable x from the parent function and defines another variable y. After that, it will call the function name nestedFunction2.

The nestedFunction2 displays the value of x from both the parent function and nestedFunction1, as well as the value of y from nestedFunction1. After executing the code, the output will show the messages from both nested functions, along with the values of x and y.

A screenshot of a computer Description automatically generated with medium confidence

Sharing Variables from Nested Functions to the Main Function

In MATLAB we can also define variables and share them from nested to main function.

function parent

nestedfunc

function nestedfunc

x = 10;

end

x = x+1;

disp(x);

end

This MATLAB code defines a function called parent that contains a nested function called nestedfunc. The code assigns a value of 10 to the variable x in nestedfunc, then increments it by 1 in the parent function and displays the result.

Nesting Multiple Functions Under the Same Parent Function

In MATLAB we can also include multiple functions within a single main parent function.

% multiple functions under same parent function

function parent

nestedfunc1

nestedfunc2

function nestedfunc1

fprintf('Linuxhint.com\n');

end

function nestedfunc2

fprintf('Welcome To Linuxhint');

end

end

This MATLAB code defines a function called parent that contains two nested functions: nestedfunc1 and nestedfunc2. When the parent function is called, it executes both nested functions. nestedfunc1 prints the message Linuxhint.com, and nestedfunc2 prints the message “Welcome To Linuxhint”.

A picture containing text, font, screenshot Description automatically generated

Sharing Variables Among Nested Functions

In MATLAB we can also define and share single-parent functions variables with two nested functions.

% two nested functions under the same parent function

function parent

x = 5

nested1

nested2

function nested1

x = x*2;

end

function nested2

x = x+5;

end

disp(x)

end

This MATLAB code defines a function called parent that declares a variable x with a value of 5. It then contains two nested functions: nested1 and nested2.

In nested1, the value of x is multiplied by 2, but since x is not explicitly passed as an argument, it creates a new local variable x within nested1 instead of modifying the outer x variable.

In nested2, the value of x is incremented by 5, also creating a new local variable x within nested2.

After executing the nested functions, the code displays the value of the outer x variable, which remains unchanged at 5 because the modifications made in the nested functions only affect the local variables within those functions and not the outer variable.

A picture containing text, screenshot, software, line Description automatically generated

Conclusion

The nested functions in MATLAB can organize code, enhance reusability, and improve performance. They allow functions to access and modify variables defined in their parent functions, enabling better code encapsulation. Nested functions reduced the need for global variables or passing multiple arguments between functions. This article covers various examples of nested functions in MATLAB.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.