Skip to Main Content Skip to Search
Product Documentation

Understanding File Locations in MATLAB

Important MATLAB Folders

When you work with files and folders, be aware of key locations that MATLAB uses.

The 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. It is not the same location as the operating system current folder.

You can always load files and execute scripts and functions that are in the current folder, even if that folder is not currently on the MATLAB search path. Functions in the current folder take precedence over functions with the same file name that reside anywhere on the search path.

Viewing and Changing the Current Folder.  You can view and change the current folder using various desktop tools and functions, as described in the following table. To specify the current folder programmatically when MATLAB starts, see Startup Folder for the MATLAB Program.

To:Do this:
Identify the Current Folder

Use one of the following:

  • The Current Folder field on the Desktop toolbar.

  • The Current Folder browser address bar — if the full path is not visible, hover over the folder icon.

  • The pwd or cd function.

Change the current folder to one you specify

Do one of the following:

  • In the Current Folder field on the Desktop toolbar, type or browse to a different folder.

  • On the Current Folder browser address bar, click an arrow that appears between portions of the path, and then choose a drive or subfolder from the drop-down list.

  • Use the cd function.

Change the current folder to a recently used folder

From the Current Folder field on the Desktop toolbar, click the down arrow, and then select a folder from the history.

Change the current folder to an active document's folder

Right-click the document tab in the Editor, and then select Change Current Folder to folder.

Document tabs appear only when more than one document is open in the Editor.

Make a subfolder the current folder

In the Current Folder browser, right-click the subfolder, and then select Open from the context menu.

Copy the Current Folder as a string
  • Select the address in the Desktop toolbar, right-click, and then select Copy.

  • Click an empty area on the right edge of the Current Folder address bar, right-click, and then select Copy.

Get help using the Current Folder address bar

Right-click an empty area on the address bar, and then from the context menu, select Help Using Address Bar

matlabroot

matlabroot is the folder where you installed MATLAB. The location differs for each installation of MATLAB. Determine its location by running the matlabroot function. When you start MATLAB, your current folder can be matlabroot, but in practice it is usually a different folder.

The Startup Folder

Each time you start MATLAB, your current folder is always the same. This location is called the startup folder. The operating system commands that runs MATLAB specifies the location of the startup folder. You can configure MATLAB to make your initial current folder a different location. For more information, see Startup Folder for the MATLAB Program.

Locations of MathWorks Products

Files and folders for products provided by MathWorks are in matlabroot/toolbox. The files and folders under matlabroot are important to your installation. In particular:

To improve performance, at the beginning of each session, MATLAB loads and caches in memory the locations of files in matlabroot/toolbox. If you make changes to files and folders in matlabroot/toolbox, running functions can produce unexpected results or generate warnings, that are related to the toolbox cache. See Toolbox Path Caching in the MATLAB Program.

To see a list of all toolbox folder names supplied with MathWorks products, run:

dir(fullfile(matlabroot, '/toolbox'))

Locations for Storing Your Files

For your convenience, MATLAB provides a folder called MATLAB to store your files. At startup, MATLAB adds the folder to the search path, allowing MATLAB to access the files stored there.

The location of the userpath MATLAB folder varies by platform and system configuration. To determine the location, run the userpath function.

On Microsoft Windows platforms, MATLAB sets the current folder to userpath at startup. On other platforms, you instruct MATLAB differently to set the current folder to userpath at startup. For more information, see Startup Folder for the MATLAB Program.

If you create subfolders within the MATLAB folder, make the new subfolders accessible to MATLAB.

If you store files in locations other than the MATLAB folder:

Path Names in MATLAB

A path name specifies file locations, for example, C:\work\my_data (on Microsoft Windows platforms) or /usr/work/my_data (on Linux or Apple Mac platforms). Path name specifications differ, depending on the platform on which you are running MATLAB. When you work with files and folders, be aware of how MATLAB uses path names and the restrictions it places on them.

Specifying Path Names on Apple Mac Platforms

When you specify path names on Mac platforms, do not use accent characters. If path names include such characters, for instance umlauts or circumflexes, the Current Folder browser and MATLAB cannot recognize the path. In addition, attempts to save a file to such a path results in unpredictable behavior.

Specifying File Separator Characters, / and \

The file separator character is the symbol that distinguishes one folder level from another in a path name.

A forward slash (/) is a valid separator on any platform. A backward slash (\) is valid only on MicrosoftWindows platforms.

In the full path to a folder, the final slash is optional.

Type filesep in the Command Window to determine the correct file separator character to use when working with files programmatically.

Specifying Absolute and Relative Path Names

MATLAB always accepts absolute path names (also called full path names), such as I:/Documents/My_Files. An absolute path name can start with any of the following:

Some MATLAB functions also support relative path names. The reference page for a function specifies the valid types of path name. Unless otherwise noted, the path name is relative to the current folder. For example:

Case Sensitivity of File Names

How MATLAB handles file names with respect to case depends on a number of factors. If you are unsure of how MATLAB will handle your specific case, it is best to specify path and case precisely when specifying a file name. The sections that follow describe how case affects common MATLAB operations.

Case Sensitivity of File Names When Calling a Function.   You call function files by specifying the file name without the file extension. MATLAB assumes you want a case-insensitive match if it cannot find a case-sensitive match on the MATLAB search path (regardless of the operating system on which MATLAB is running).

Furthermore, if multiple files with the same name, but different extensions exist in the same folder, then MATLAB searches among the files in the folder in this precedence order:

For details on precedence, see File Precedence in the Programming Fundamentals documentation.

For example, suppose myfile.m is on the search path, but MYFILE.M is not. If you type MYFILE at the MATLAB command prompt, then MATLAB runs myfile.m, but warns you that there is a case mismatch and advises you that this warning will become an error in a future release.

If myfile (a MEX-file) and MYFILE.m are on the search path, and you type MYFILE at the command prompt, MATLAB runs MYFILE.M, even if myfile is higher on the search path.

To see which file MATLAB will use without running that file, use which with the —all option. For example, which myfile.m —all.

Case Sensitivity of File Names When You Load a MAT-File.  When you call load and specify a file without an extension, MATLAB searches for a MAT-file. Case-sensitivity depends on the operating system where MATLAB is running, as follows:

Case Sensitivity of File Names When You Save a MAT-File.  When you call save and specify a MAT-file without an extension, MATLAB saves the file to the current folder. Case-sensitivity depends on the operating system where MATLAB is running, as follows:

Maximum Length of Path Names in MATLAB

The maximum length allowed for a path name depends on your platform.

For example, on Microsoft Windows platforms:

If you get unexpected results when working with long path names, use absolute instead of relative path names. Alternatively, use shorter names for folders and files.

Constructing Path Names on Different Platforms

Use fullfile to construct path names in statements that work on any platform. This function is particularly useful when you provide code to someone using it on a platform different from your own. The ismac, ispc, and isunix functions identify the platform you are currently using.

Including Spaces in Path Names

When a function argument is a file or path name, and the name includes spaces, use the function syntax. For example:

delete('temp file.m')   % Function syntax works for a file name containing a space

The command syntax does not work. For example:

delete temp file.m   % Command syntax does NOT work for a file name containing a space

Partial Path Names in MATLAB

A partial path name is the last portion of a full path name for a location on the MATLAB search path.

Some functions accept partial path names. The reference page for a function typically specifies the valid types of path names.

Examples of partial path names are: matfun/trace, private/cancel, and demos/clown.mat.

Use a partial path name to:

Be sure to specify enough of the path name to make the partial path name unique.

See Also

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS