In MATLAB, "changing the location" has two primary meanings: changing the current working directory for the active session, or changing the permanent search path so MATLAB can find files and functions in specific folders.
There are several ways to achieve both, ranging from simple graphical user interface (GUI) actions to command-line functions.
Changing the current folder
The current folder, or current working directory, is the folder where MATLAB looks for files first. It is displayed in the "Current Folder" pane of the MATLAB desktop. All files, scripts, and functions you run from the command window are assumed to be located in this directory unless you specify a full path.
GUI method
- Use the "Current Folder" toolbar: Click the button with the tooltip "Browse for folder" next to the current folder display.
- Navigate and select: Use the file browser to navigate to your desired folder and click Select Folder. The current folder location will update immediately.
Command-line method
- Use the
cdcommand: In the Command Window, use thecd(change directory) command followed by the desired folder path.- Absolute path:
cd('C:\Users\YourUsername\Documents\MatlabProjects')(Windows) orcd('/home/user/matlab_files')(Linux/Mac). - Relative path:
cd('my_project')ifmy_projectis a subfolder of your current directory. - Up one directory:
cd('..')moves up one level in the directory tree.
- Absolute path:
- Verify the change: Run
pwd(print working directory) in the Command Window to confirm the new location.
Changing the MATLAB search path
The search path is a set of directories where MATLAB looks for files and functions. Files in the current folder take precedence, but if a function or file is not found there, MATLAB will search the path folders. Changes to the search path are crucial for permanently adding locations containing your custom functions or toolboxes.
GUI method using the "Set Path" dialog
- Open the dialog: On the Home tab, in the Environment section, click Set Path.
- Add folders: In the "Set Path" dialog box, click Add Folder... to add individual folders, or Add with Subfolders... to add a directory and all folders inside it.
- Arrange and save: Use the Move Up and Move Down buttons to order the path, as the order determines search precedence. Click Save to make the changes permanent for future sessions. In MATLAB Online, changes are saved automatically.
Command-line method for the search path
- Use
addpath: Theaddpathfunction adds a specified folder to the search path for the current session.addpath('C:\my_matlab_code').addpath(genpath('C:\my_matlab_code'))to add a folder and all its subfolders.
- Make changes permanent with
savepath: After usingaddpath, runsavepathto store the updated path in thepathdef.mfile. This ensures the change persists across MATLAB sessions.
Changing the default startup folder
You can configure MATLAB to always start in a specific folder. This is useful for projects you frequently work on and can be set in the preferences.
GUI method using "Settings"
- Open Settings: On the Home tab, in the Environment section, click Settings.
- Navigate to General: In the "Settings" window, select MATLAB > General.
- Set the startup folder: For "Initial working folder," select the custom path option and enter your desired directory. You can also click the browse button to select a folder.
- Save and restart: Click OK and restart MATLAB for the new settings to take effect.
Method via a startup.m file
A startup.m script is a file that MATLAB runs automatically when it starts. This provides a flexible, code-based way to configure your environment.
- Create the file: In your default startup folder (e.g.,
C:\Users\YourUsername\Documents\MATLAB), create a new script namedstartup.m. - Add commands: Add a
cdcommand to thestartup.mfile to change the directory on startup. For example:cd('C:\MyProjects\AwesomeProject').- Caution: A
startup.mfile withcdwill override the "Initial working folder" setting in the Preferences.
- Caution: A
Location change scenarios and best practices
Understanding the different ways to change location is key to managing your MATLAB projects effectively.
- For temporary work on a specific project: Use the
cdcommand or the current folder toolbar to change your working directory for the duration of your session. - For reusable functions and toolboxes: Use
addpathandsavepath, or the "Set Path" GUI, to add the directory containing your functions to the MATLAB search path. This allows you to call those functions from any current working directory. - For a primary, long-term project: Change the default startup folder in Settings to point to your main project directory. This ensures you start every MATLAB session in the right place.
- For multiple projects: Using MATLAB Projects is a robust way to manage multiple project-specific paths and settings. A project defines a unique path that is active only when that project is loaded.
- For shared computers: Avoid using
savepathif you do not have write access to the system-widepathdef.mfile. Instead, add paths withaddpathin astartup.mfile located in your user-specific MATLAB folder.