Specify File Names - MATLAB & Simulink (original) (raw)

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 asI:/Documents/My_Files or/users/myuserid/Homework/myfile.m. An absolute path name can start with any of the following:

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

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 .

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, anddemos/clown.mat.

Use a partial path name to:

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:

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.

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.

See Also

filesep | fullfile | which | ismac | ispc | isunix

Topics