
- 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 - Local Functions
MATLAB functions serve as reusable code blocks designed to execute particular tasks or computations. They play a crucial role in structuring and modularizing code within MATLAB, thereby enhancing its readability. Functions improve code clarity by packaging complex operations into a clearly named entity. This improved code organization permits you to invoke the same function numerous times with varying inputs, promoting code reusability. Additionally, functions contribute to code maintainability and readability, simplifying the process of debugging and updating your MATLAB programs.
Creating a function in MATLAB is straightforward. You can start by using the function keyword, followed by the output variables in square brackets, the function name, input variables in parentheses, and then the function body enclosed in curly braces. Here's a simple example −
function output = myFunction(input) % Function body output = input * 2; end
In this example, myFunction is the name of the function, input is the input variable, and output is the output variable. You can replace the function name and variable names with your specific names and define the function's functionality within the body.
Now that we know what functions are in Matlab , let us take a look at Local Functions in Matlab.
Local functions in MATLAB are functions that are defined within the scope of another function or script.These functions are not visible or accessible from outside the parent function or script, making them a useful tool for creating modular and organized code. Let us explore the concept of local functions in MATLAB with detailed explanations and examples.
Why do we Need Local Functions?
Local functions comes with several advantages which are listed below −
- You can break down a complex task into smaller, more manageable parts by using local functions. This promotes code organization and makes your code easier to read and maintain.
- Local functions are encapsulated within the parent function's scope, so they can access and modify the parent function's variables, making them suitable for implementing helper functions.
- Local functions are hidden from the global workspace, preventing accidental clashes with other function names in your MATLAB environment.
Defining Local Functions
To define a local function in MATLAB, you place the function definition within the body of another function or script. Here is the syntax for it.
Syntax for Local Function
function parentFunction(inputArgs) % Parent function code % Local function definition function localFunction(inputArgs) % Local function code end % More parent function code end
Function parentFunction(inputArgs) − This line defines the parent function named parentFunction. It takes inputArgs as input arguments, which can be any number of input arguments that the parent function requires.
% Parent Function Code − This is a comment line, denoted by %, which is ignored by MATLAB. It is used to describe or document the purpose of the parent function. The actual code of the parent function goes here.
Function localFunction(inputArgs) − This line defines a local function named localFunction within the scope of the parent function. Local functions are nested inside the parent function and can access variables and input arguments of the parent function.
inputArgs − Local functions can have their own input arguments, distinct from the parent function's input arguments. These arguments allow you to pass data specific to the local function.
% Local Function Code − Similar to the comment line in the parent function, this comment line describes or documents the purpose of the local function. The actual code of the local function goes here.
% More Parent Function Code − After the local function definition, you can include additional code for the parent function. This code executes after the local function definition and can use both the input arguments and local functions within its scope.
Examples of Local Functions
Here are some examples of local functions −
Example 1: Simple Local Function
In this example, we have a parent function calculateAverage that calculates the average of two numbers using a local function sumNumbers.
function average = calculateAverage(a, b) % Local function to calculate the sum of two numbers function sumResult = sumNumbers(x, y) sumResult = x + y; end % Calculate the sum total = sumNumbers(a, b); % Calculate the average average = total / 2; end

The call to calculateAverage function is as follows −
>> result = calculateAverage(5, 7) result = 6
Example 2: Local Function with Multiple Inputs
In this example, we have a parent function calculateHypotenuse that calculates the hypotenuse of a right triangle using a local function squareAndSum.
function hypotenuse = calculateHypotenuse(a, b) % Local function to square and sum two numbers function sumResult = squareAndSum(x, y) sumResult = x^2 + y^2; end % Calculate the squared sum squaredSum = squareAndSum(a, b); % Calculate the hypotenuse hypotenuse = sqrt(squaredSum); end
In matlab the function is saved as follows −

You can call the calculateHypotenuse function as follows −
>> result = calculateHypotenuse(3, 4) result = 5