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.

Need Expert Guidance with MATLAB or Simulink?

Our 500+ PhD engineers provide verified MATLAB code, custom Simulink models, and 1-on-1 tutoring with Turnitin plagiarism reports.

Verified Feedback

What Engineering Students Say

Real feedback from students across top engineering universities worldwide.

Verified Student

“I got full marks on my MATLAB DSP assignment! The filter design code was completely vectorized, the frequency response plots were exact, and the delivery was 8 hours before my deadline. Highly recommended!”

AS

Aditi Sharma

IIT Bombay • Signal Processing Coursework
Verified Student

“Our Simulink EV powertrain model had severe algebraic loop and solver errors. The MATLABSolutions team fixed the solver configuration in 4 hours and provided an annotated scope diagram. Lifesaver for my final year!”

JM

John M.

Monash University, Australia • Simulink Dynamic Model
Technical Knowledge Base

Latest MATLAB Guides & Tutorials

Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.

MATLAB Guide 5 Min Read

AI-Tuned Adaptive PI and Fractional-Order Control for Hybrid Microgrid Inverters in MATLAB

Modern renewable energy grids face a major operational hurdle: the loss of rotational inertia. As solar photovoltaics, wind energy, and battery energy storage s...

MATLAB Guide 5 Min Read

Solving 6-DOF Aircraft & Quadrotor Flight Dynamics in MATLAB: ode45 vs. ode15s and Quaternion Kinematics

Simulating a 6-Degree-of-Freedom (6-DOF) flight vehicle in MATLAB looks straightforward on paper. You set up Newton-Euler equations of motion, calculate aerodynamic...