How to Use addpath function in MATLAB: Add Folders to Search Path Guide

MATLAB Illustration

Using the addpath Function in MATLAB

The addpath function adds folders to MATLAB's search path, allowing you to run scripts/functions from those locations without specifying full paths. MATLAB searches the path for .m files when you call a function.

Syntax

MATLAB
addpath(folderName1, folderName2, ..., '-begin' | '-end')  % Default: '-begin' (top of path) addpath(..., '-frozen')  % Disables change detection oldPath = addpath(...)   % Returns path before adding

Step-by-Step Guide

  1. Add a Single Folder:
    MATLAB
    addpath('C:UsersYourNameDocumentsMATLABMyFolder')  % Adds to top
  2. Add Multiple Folders:
    MATLAB
    addpath('folder1', 'folder2', '-end')  % Adds to bottom
  3. Add Folder + All Subfolders (use genpath):
    MATLAB
    addpath(genpath('C:MyProject'))  % Includes all subfolders
  4. Make Permanent:
    • Run savepath to save for future sessions.
    • Or add addpath commands to startup.m (in matlabroot/toolbox/local).
  5. Remove Folders:
    MATLAB
    rmpath('C:MyFolder')
  6. View Current Path:
    MATLAB
    path  % Displays full pat

Examples

  • Temporary Add for Testing:
    MATLAB
    addpath('.utils');
    % Relative to current directory myFunction();
    % Now callable rmpath('.utils');
    % Clean up
  • Project Setup Script:
    projectRoot = pwd;
    MATLAB
    addpath(genpath(fullfile(projectRoot, 'src')));
    addpath(genpath(fullfile(projectRoot, 'tools')));
    savepath;

Tips & Best Practices

  • Use full paths to avoid issues across machines/OS.
  • genpath skips folders like @class, +package, private.
  • Check if added: which myFunction (shows path if found).
  • Common Error: "Undefined function" → Add the folder!
  • GUI Alternative: Home tab → Set Path → Add Folder → Save.
  • Why Use It? MATLAB only finds files in current folder or on path.

What Our Students Say

★★★★★

“I got full marks on my MATLAB assignment! The solution was perfect and delivered well before the deadline. Highly recommended!”

Aditi Sharma, Mumbai
★★★★☆

“Quick delivery and excellent communication. The team really understood the problem and provided a great solution. Will use again.”

John M., Australia

Latest Blogs

Explore how MATLAB Solutions has helped clients achieve their academic and research goals through practical, tailored assistance.

How to Solve Differential Equations in MATLAB (ode45, ode15s, bvp4c)

Differential equation assignments usually boil down to three scenarios: standard initial value problems, stiff systems that crash normal solvers, and boundary value problems where conditions are split between two ends of a domain. This guide walks through how to pick the right MATLAB solv

Physics-Informed Neural Networks (PINNs) in MATLAB: Complete Guide

1. Why Standard AI Fails on Real-World Physics Problems If you've ever tried training a standard deep learning model to predict fluid dynamics, structural stress, or heat transfer, you've likely hit a wall. Standard neural networks excel at recognizing patterns in images or text, but wh