|On this page…|
When you work with files and folders, be aware of key locations that MATLAB® uses.
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 MATLAB Startup Folder.
|Identify the current folder|
Use one of the following:
|Change the current folder to one you specify|
Do one of the following:
|Change the current folder to a recently used folder|
From the current folder 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.
|Make a subfolder the current folder|
In the Current Folder browser, right-click the subfolder, and then select Open.
|Copy the current folder as a string|
Click an empty area on the right edge of the Current Folder address field, right-click, and then select Copy.
|Get help using the current folder toolbar|
Right-click an empty area on the address bar, and then select Help Using Address Field
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.
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 MATLAB Startup Folder.
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:
Do not store your personal files and folders in matlabroot/toolbox.
Do not change files, folders, and subfolders in matlabroot/toolbox. The exception is the pathdef.m file, which you can update and save in its default location, matlabroot/toolbox/local.
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 MATLAB.
To see a list of all toolbox folder names supplied with MathWorks products, run:
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 MATLAB Startup Folder.
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:
Make the files accessible to MATLAB by adding their folders to the search path.
Do not store the files in the folders provided for MathWorks products.
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.
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.
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 when working with files programmatically.
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:
UNC path '\\' string
Drive letter, on Microsoft Windows platforms, such as C:\.
'/' character on Linux platforms
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:
/myfolder refers to the myfolder folder in the current folder and myfile.m refers to the myfile.m file 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 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 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
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:
MDL (Simulink® model) files
MATLAB code files (those with a .m extension)
For details on precedence, see Function Precedence Order.
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:
If you attempt to load MYFILE, and MYFILE.MAT is anywhere on the MATLAB search path, then MATLAB loads MYFILE.MAT. This is true, even if myfile.mat, and myfile.m are higher than MYFILE.MAT on the MATLAB search path.
If you attempt to load MYFILE, and MYFILE.MAT is not on the MATLAB search path, then MATLAB returns the message, Unable to read file MYFILE.MAT: No such file or directory. This is true even if myfile.mat is on the MATLAB search path.
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 you that there is a case mismatch.
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:
MATLAB saves the file using the case you specify. Two files with the same name, but different cases can exist in the same folder.
Because the Windows operating system considers two files with the same name to be the same file (regardless of case), you cannot have two files with the same name in the same folder. If you save MYFILE, and myfile.mat already exists in the current folder, then MYFILE.MAT replaces myfile.mat without warning. If you save myfile, and MYFILE.mat already exists in the current folder, the contents of myfile.mat replace the contents of MYFILE.mat, but the name remains MYFILE.mat.
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.
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.
When a function argument is a file or path name, and the name includes spaces, use the function syntax. For example:
load('filename with space.mat')
The command syntax does not work for a file name containing a space. For example:
load filename with space.mat % Command syntax does NOT work for a file name containing a space
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:
Specify a location more conveniently than by using the full path name.
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 get help for the set function in the Database Toolbox™ product, type:
Locate method files. For example, to get help for the time series object plot method type:
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.