MATLAB - Sub-functions



Sub-functions in MATLAB are a powerful feature that allows you to break down a larger function into smaller, modular components. These smaller components, or sub-functions, can be defined within the same .m file as the main function and are designed to perform specific, localized tasks. Using sub-functions can greatly enhance code organization, readability, and reusability.

Creating Sub-Functions

Follow the steps given below to create sub-functions in matlab.

Step 1 − First define the main function that you want inside the .m file.

Step 2 − Below the main function, define one or more sub-functions using the function keyword.

Step 3 − Here is an example for the same with the main function and sub-function inside a .m file.

Consider you want to create a MATLAB function to calculate the area of a rectangle. You can define the main function calculateRectangleArea and a sub-function areaOfRectangle as follows −

% Main function
function area = calculateRectangleArea(length, width)
   area = areaOfRectangle(length, width);
end

% Sub-function
function A = areaOfRectangle(l, w)
   A = l * w;
end

Inside matlab the calculateRectangleArea.m file will be as follows −

area of rectangle

In the example above, areaOfRectangle is a sub-function used within the main function calculateRectangleArea. The sub-function encapsulates the logic for calculating the area, promoting code modularity.

Step 4 − Let us execute the above function inside matlab command window as shown below −

result = calculateRectangleArea(4, 5)

The output is −

>> result = calculateRectangleArea(4, 5)

result =

    20

Scope of Sub-Functions

Sub-functions have a limited scope and are only accessible from within the main function in the same .m file. They are not visible from outside the file. This encapsulation helps prevent naming conflicts and allows you to focus on specific tasks within each function.

Using Multiple Sub-Functions in Your Code

Let us consider a little more complex example that shows the scope of sub-functions inside the main function. Here you want to calculate the volume of a rectangular prism. You can create a main function and multiple sub-functions to handle different aspects of the calculation −

% Main function
function volume = calculatePrismVolume(length, width, height)
   baseArea = areaOfRectangle(length, width);
   volume = volumeOfPrism(baseArea, height);
end

% Sub-function 1
function A = areaOfRectangle(l, w)
   A = l * w;
end

% Sub-function 2
function V = volumeOfPrism(baseArea, h)
   V = baseArea * h;
end

In this example, areaOfRectangle and volumeOfPrism are sub-functions, each of them responsible for calculating the Area of rectangle and volume of the prism.

Let us see the display of calculatePrismVolume.m in matlab .m script file.

calculate prism volume

Let us execute it now in matlab command window as shown below −

>> result = calculatePrismVolume(4, 5, 6)

result =

   120

Local Functions in Matlab

In MATLAB, "sub-functions" and "local functions" refer to the same concept. Both terms are often used interchangeably to describe functions that are defined within the same script or function file and have a limited scope, accessible only from within the same file. These functions are used to break down a larger function into smaller, modular components.

Whether it is sub-functions or local functions, their primary characteristics are −

  • They are defined within the same script or function file as the main function.
  • They have a limited scope and can only be accessed from within the same file.
  • They are used to perform specific, localized tasks within the context of the main function.
  • They enhance code modularity, organization, and readability by encapsulating related functionality.

Advantages of Using Sub-Functions

  • Sub-functions help break down complex tasks into smaller, manageable pieces, enhancing code modularity and reusability.
  • Code is more readable as each sub-function focuses on a specific task, making it easier to understand.
  • Sub-functions provide a level of encapsulation, keeping implementation details hidden from the main function and other functions.
  • Sub-functions help organize your code by keeping related functions together in a single .m file.
  • Common tasks can be encapsulated in sub-functions, reducing redundancy.

Difference between Sub-functions or Local Functions and Nested Functions

Sub-functions or Local functions and nested functions in MATLAB serve similar purposes in terms of code modularity and organization, but they have some important differences in terms of scope and accessibility.

Sub-functions Nested functions
Sub-functions have a limited scope and are only accessible from within the main function or script in which they are defined.Sub-functions cannot be accessed from outside the file in which they are defined.They are useful for encapsulating logic within a specific function or script, making it more modular. Nested functions have a broader scope. They are accessible from within the enclosing function and any other nested functions within the same containing function.They can also access variables from the enclosing function directly, without needing to pass them as arguments.Nested functions are essentially private functions within a function and have access to the workspace of the containing function.
Sub-functions are typically defined in the same file as the main function, and they are often used to break down a complex main function into smaller, modular components.They can be used to organize code within a single file. Nested functions are defined within an enclosing function, creating a hierarchical structure within a single .m file. This allows you to encapsulate functionality while maintaining a clear hierarchy.
Sub-functions are generally not accessible from external code or other functions outside the file in which they are defined. Nested functions are not accessible from external code or functions outside the enclosing function. They are essentially private to the enclosing function.
Advertisements