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 −

private function

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 −

public function exmple

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.

Advertisements