Main Content

Specify File Names

Construct Valid Path and File Names

Specify Path Names

A path name specifies file locations, for example, C:\work\my_data (on Microsoft® Windows® platforms) or /usr/work/my_data (on Linux® or Mac platforms). If you do not specify a path name when accessing a file, MATLAB® first searches in the current folder. To indicate a file in a particular location, specify a path name.

Path name specifications differ, depending on the platform on which you are running MATLAB. Use the fullfile function 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 other than your own.

fullfile inserts platform-specific file separators where necessary. 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 Microsoft Windows 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 on your platform.

To identify the platform on which MATLAB is currently running, use the ismac, ispc, and isunix functions.

Characters Within File and Folder Names

File names must start with a letter, and can contain letters, digits, or underscores.

Avoid using accent characters such as umlauts or circumflexes in path names. MATLAB might not recognize the path. In addition, attempts to save a file to such a path might result in unpredictable behavior.

If a path or file name contains spaces, enclose the input in single quotes. For example:

load 'filename with space.mat'
or
load('filename with space.mat')

Absolute and Relative Path Names

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

  • UNC path '\\'.

  • Drive letter, on Microsoft Windows platforms, such as C:\.

  • '/' character on Linux platforms.

Some MATLAB functions also support relative path names. Unless otherwise noted, the path name is relative to the current folder. For example:

  • myfile.m refers to the myfile.m file in the current folder.

  • myfolder refers to the myfolder folder in the current folder.

  • ../myfolder/myfile.m refers to the myfile.m file in the myfolder folder, where myfolder is at same level as the current folder. Each repetition of ../ at the beginning of the path moves up an additional folder level.

Tip

If multiple documents are open and docked in the Editor, you can copy the absolute path of any of these documents to the clipboard. This practice is useful if you need to specify the absolute path in another MATLAB tool or an external application. Right-click the document tab, and then select Copy Full Path to Clipboard.

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.

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

Use a partial path name to:

  • Specify a location independent of where MATLAB is installed.

  • Locate a function in a specific toolbox when multiple toolboxes contain functions with that name. For example, to open the file for the set function in the Database Toolbox™ product, type:

    open database/set
  • Locate method files. For example, to check if a plot method exists for the time series object, type:

    exist timeseries/plot

    Specifying the at sign character (@) in method folder names is optional.

  • Locate private and method files, which sometimes are hidden.

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

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:

  • The maximum length is known as MAX_PATH.

  • You cannot use an absolute path name that exceeds 260 characters.

  • For a relative path name, you might need to use fewer than 260 characters. When the Windows operating system processes a relative path name, it can produce a longer absolute path name, possibly exceeding the maximum length.

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.

Case Sensitivity of File Names

In general, it is best to specify path and case precisely when specifying a file name.

Case Sensitivity When Calling Functions

You call function files by specifying the file name without the file extension. MATLAB returns an error if it cannot find a case-sensitive match on the search path. By default, MATLAB suggests a function with the correct case.

When multiple files have the same name, MATLAB follows precedence rules to determine which to call. For more information, see Function Precedence Order.

Case Sensitivity When Loading and Saving Files

Linux platforms File names are case sensitive.

  • When loading or reading from a file, specify the file name using the correct case.

  • When saving or writing to a file, MATLAB saves the file in the case you specify. Two files with the same name, but different cases can exist in the same folder.

Windows platforms — File names are case insensitive. The Windows operating system considers two files with the same name to be the same file, regardless of case. Therefore, you cannot have two file names that differ only by case in the same folder.

  • When loading or reading from a file, MATLAB accesses the file with the specified name that is higher on the search path, regardless of case. For example, if you attempt to load MYFILE and myfile.mat is higher on the search path than MYFILE.MAT, then MATLAB loads myfile.mat without warning that there is a case mismatch.

  • When saving or writing to a file, if you specify a file name that already exists in the folder, MATLAB accesses the existing file without warning. For example, if you save data to a file named myfile using the save function, and MYFILE.mat already exists in the folder, the data replaces the contents of MYFILE.mat. However, the file name remains MYFILE.mat.

See Also

| | | | |

Related Topics