cd

Shows or changes the current folder.

Category: Programming > Files and Folders

Syntax

cd

Displays the current folder.

cd(folder)

Changes the current folder to folder.

oldfolder = cd(folder)

Changes folder and returns the previous one.

Description

The current folder is where relative file names are resolved and where function files are searched first. cd is how a script moves between data folders. Saving the returned oldfolder makes it easy to go back with cd(oldfolder).

With no output argument and no input, cd prints the current folder. Changing to a folder that does not exist is an error.

Input arguments

folder
Folder name as text, relative or absolute. '..' is the parent folder.

Output arguments

oldfolder
Full path of the folder that was current before the call.

Example

Entering a folder and coming back

mkdir work
old = cd('work');
[~, name] = fileparts(pwd)
cd(old);
strcmp(old, pwd)
Output
name = 'work'
ans = logical
   1

See also

pwd mkdir dir addpath ls