
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
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 −

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.

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