To run this file you can either change the MATLAB current folder

—Oops, on re-reading your question, my answer isn’t very helpful. I’ll leave it below since it is somewhat relevant.

I usually add to path unless the data for the specified script is in the same folder. If the script doesn’t rely on data, or you will be manually entering data or selecting it from somewhere else, this is usually the safest option.

I always considered a folder change to be a temporary thing, such as working on a dataset that resides in a particular folder, and a path addition to be a more permanent change, such as adding a new toolbox or set of functions to Matlab. However, if you don’t call savepath, then the changes will revert upon shutdown. So if you are going to make a function that you will be using Long-term, either save it in the current Matlab path or add/save the enclosing folder to the path. If you have some data that you need to analyze for a class, just change to that folder temporarily, otherwise Matlab will spend time searching through the folder 3 years later when the data is inconsequential.

Main Content

Syntax

Description

example

cd displays the current folder.

example

cd newFolder changes the current folder to newFolder. Folder changes are global. Therefore, if you use cd within a function, the folder change persists after MATLAB® finishes executing the function.

example

oldFolder = cd(newFolder) returns the existing current folder to oldFolder, and then it changes the current folder to newFolder.

Examples

collapse all

Change Current Folder Using Full and Relative Paths

Change the current folder to the featured examples folder for MATLAB R2017a, assuming that version is installed on your C: drive.

cd 'C:\Program Files\MATLAB\R2017a\examples\matlab_featured'

Using the full path, change the current folder from C:\Program Files\MATLAB\R2017a\examples\matlab_featured to C:\Program Files\MATLAB\R2017a.

Use a relative path to change the current folder from C:\Program Files\MATLAB\R2017a back to C:\Program Files\MATLAB\R2017a\examples\matlab_featured.

cd examples\matlab_featured

Change, and then Restore Current Folder

Change the current folder to the featured examples folder for MATLAB R2017a, assuming that version is installed on your C: drive. Then restore the current folder to its original location.

Change the current folder to the featured examples folder for MATLAB R2017a.

cd 'C:\Program Files\MATLAB\R2017a\examples\matlab_featured'

Change the current folder to C:\Program Files, saving the folder path before changing it.

oldFolder = cd('C:\Program Files')

oldFolder =

    'C:\Program Files\MATLAB\R2017a\examples\matlab_featured'

Use the cd command to display the new current folder.

Change the current folder back to the original folder, using the stored path. Use the cd command to display the new current folder.

C:\Program Files\MATLAB\R2017a\examples\matlab_featured

Change Current Folder to User Home Folder on UNIX®

Change the current folder to the featured examples folder for MATLAB R2016b, assuming that version is installed in your user home folder on a UNIX platform.

cd ~/MATLAB/R2016b/examples/matlab_featured

Input Arguments

collapse all

newFolder — New folder path character vector | string scalar

New folder path to which you want to change the current folder, specified as a character vector or string scalar. If newFolder is a string, enclose it in parentheses. For example, cd("FolderName").

Valid values include a full or relative path or one of these values.

../ One level up from the current folder.
Multiples of ../ Multiple levels up from the current folder.
./ A path relative to the current folder. cd assumes that the path is relative to the current folder even if the ./ is not present.

If newFolder contains spaces, enclose it in single quotation marks. For example, cd 'Folder Name'.

On UNIX® systems, you can use the ~ (tilde) character to represent the user home folder. For example, ~/ or ~username/.

MATLAB resolves folder path names containing '.', '..', and symbolic links to their target location before changing the current folder. For example, if you specify c:\matlab\..\work, MATLAB resolves the path name to c:\work before changing the current folder.

Output Arguments

collapse all

oldFolder — Previous current folder character vector

Previous current folder that was in place before you issued the cd command, returned as a character vector.

More About

collapse all

Current Folder

The current folder is a reference location that MATLAB uses to find files. This folder is sometimes referred to as the current directory, current working folder, or present working directory.

Extended Capabilities

Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Usage notes and limitations:

  • When used to display the current folder, cd supports thread-based environments.

  • When used to change the current folder, cd does not support thread-based environments.

For more information, see Run MATLAB Functions in Thread-Based Environment.

Version History

Introduced before R2006a

expand all

R2022a: Leading whitespace characters in drive letter paths on Windows cause error

On Windows machines, leading whitespace characters in the input path are no longer trimmed. Input paths containing leading whitespace characters cause an error to be thrown instead.

  • Trial Software
  • Trial Software
  • Product Updates
  • Product Updates

How can you change the path of the current directory folder in MATLAB?

Folder changes are global. Therefore, if you use cd within a function, the folder change persists after MATLAB® finishes executing the function. oldFolder = cd( newFolder ) returns the existing current folder to oldFolder , and then it changes the current folder to newFolder .

What is the current folder in MATLAB?

The Current Folder browser enables you to interactively manage files and folders in MATLAB®. Use the Current Folder browser to view, create, open, move, and rename files and folders in the current folder.