Solved: add to path

The process of adding to a path in MATLAB involves modification of the MATLAB search path. The MATLAB search path is a list of directories that MATLAB goes through when it receives a function or script call. By adding directories to the path, MATLAB can access scripts/functions stored within those directories from any location. This action is vital when using custom scripts or functions from different directory locations.

Problem Description

Consider a scenario where there are MATLAB scripts and functions stored in various directories. Some of these scripts may need to access functions stored in other directories. To prevent errors or manually specifying absolute paths every time a script is called, we can add those directories to the MATLAB path.


The ‘addpath’ function in MATLAB helps solve this exact problem. It adds specified folders to the MATLAB search path, allowing the user to call any script or function contained within these folders from any location.

% example of addpath

This function call adds the ExampleFolder directory to the MATLAB search path.

Explaining the Code

The ‘addpath’ function accomplishes the task of adding a directory to the MATLAB path. It takes a string as an argument which is the path to the directory that is to be added.

-The quotes(‘ ‘) in the function define a string.
-The path to the directory is written between these quotes.
-The slashes are escape sequences, specifying the directory hierarchy.
-It starts from the disk drive (C), follows through Users, Username, Desktop, and finally to ExampleFolder.

After running this line of code, any script or function stored in ExampleFolder can be called directly, without specifying absolute paths.

Important MATLAB Path Functions

Other than ‘addpath’, MATLAB provides various other functions to manage the search path:

  • ‘rmpath’: Removes the specified directories from the MATLAB search path.
  • ‘path’: Displays the current MATLAB search path.
  • ‘savepath’: Saves the current MATLAB search path for future MATLAB sessions.
  • ‘userpath’: Returns the user-defined portion of the MATLAB search path.

Each of these functions helps manage the MATLAB search path, which is crucial for organising and utilising scripts and functions stored across different directories. Understanding and effectively using these search path functions are essential skills for any efficient MATLAB programmer.

Further Notes

While MATLAB automatically saves the path changes for the current session, these changes are not permanent and will reset once MATLAB restarts. To make these changes permanent across all sessions, ‘savepath’ function should be called right after making any modifications to the search path.

Related posts:

Leave a Comment