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.
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

Physics-Informed Neural Networks (PINNs) for Microgrid Dynamics and Power Flow in MATLAB

Modern microgrids operate with low physical inertia, rapid inverter switching dynamics, and intermittent renewable power generation. Simulating these systems requir...

MATLAB Guide 5 Min Read

ROS 2 and MATLAB Co-Simulation for Autonomous Mobile Robot Path Tracking Using NMPC

Tracking complex trajectories with non-holonomic mobile robots requires handling physical constraints such as actuator saturation, wheel slip, and sharp cornering. Standard cont...