
- 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 - Private Functions
In MATLAB, private functions are an essential concept for organizing and modularizing your code.They allow you to create functions that are only accessible within the scope of the parent function or the containing folder, making it easier to manage and maintain your codebase.
What are Private Functions?
Private functions in MATLAB are functions that are not visible or accessible from outside their parent function or containing folder. They are used to encapsulate code that is only relevant and intended for use within a specific function or a set of related functions. This encapsulation helps improve code organization, readability, and maintainability.
Private functions in MATLAB serve a valuable purpose in constraining the accessibility of a function. By categorizing a function as private, you achieve this designation by placing it within a subdirectory labeled 'private.' Consequently, the function becomes exclusively accessible to functions and scripts situated in the directory directly superior to the private subdirectory.
Let us now understand how to create and access private functions.
Creating and Accessing Private Functions
Follow the steps to create a private function inside a private folder and access it in other public functions.
- Consider you current working folder as: /MATLAB Drive .Inside it create a folder private.
- Inside a folder on the /MATLAB Drive, create a subfolder named "private" without adding it to the MATLAB path. Inside the "private" folder, create a MATLAB function file named "privateFunctionExample.m" with the following content −
function privateFunctionExample % PRIVATEFUNCTIONEXAMPLE An example of a private function. disp('You found the private function.');
In matlab the display is as follows −

Outside of the "private" folder, create a MATLAB function file named "publicFunctionExample.m" with the following content −
function publicFunctionExample privateFunctionExample
In matlab the display is as follows −

This "publicFunctionExample" function is not designated as private and is accessible from outside the folder structure. Inside the "publicFunctionExample" function, it calls the "privateFunctionExample" function defined within the "private" folder.
Change your current folder to any location that is not within the " /MATLAB Drive /private" folder, and then call the "publicFunctionExample" function −
publicFunctionExample
Inside matlab command window the output is as follows −
>> publicFunctionExample You found the private function. >>
When you run this code, MATLAB executes the "publicFunctionExample" function, which, in turn, calls the "privateFunctionExample" function defined as a private function. As a result, you'll see the output: You found the private function.
You can access the help documentation for the private function "privateFunctionExample" by using the help command with the full path to the private function −
help private/privateFunctionExample
When you execute inside matlab command window the output is −
>> help private/privateFunctionExample privateFunctionExample An example of a private function.
Difference between Private Function and Standard Function
Let us list out the differences between private function and standard function that we most commonly use.
Private Function
Private functions are only accessible within the function or script in which they are defined or from other functions located in the same folder, often a "private" subfolder. They are not visible or accessible from outside this limited scope. This makes them suitable for encapsulating implementation details or internal helper functions.
Private functions have a limited scope, making them suitable for encapsulating and hiding specific implementation details within a parent function or script. They promote modularity and protect internal logic from external interference.
Private functions are designed for encapsulation. They encapsulate and hide the details of an internal process, allowing you to maintain a clean and organized code structure.
Standard Function
Standard functions are accessible from anywhere within the MATLAB environment, provided the function's path is in the MATLAB search path or the function resides in the current folder. These functions can be called from any script, function, or command window.
Standard functions have a broader scope and can be used across different parts of your MATLAB codebase, making them suitable for functions with broader applicability that need to be accessed from various scripts and functions.
Standard functions do not inherently provide encapsulation, as they are meant to be accessible from various parts of your codebase.