Skip to Main Content Skip to Search
Product Documentation

fileparts - Parts of file name and path

Syntax

[pathstr, name, ext] = fileparts(filename)

Description

[pathstr, name, ext] = fileparts(filename) returns the path name, file name, and extension for the specified file. The file does not have to exist. filename is a string enclosed in single quotes. The returned ext field contains a dot (.) before the file extension.

Tips

Input Arguments

filename

String containing a name of a file or folder, which can include a path and file extension. The function interprets all characters following the right-most delimiter as a file name plus extension.

Output Arguments

pathstr

String containing the part of filename interpreted as a path name

name

String containing the name of the file without any extension

ext

String containing the file extension only, beginning with a period (.)

Definitions

Path Name

The full or partial path to a destination folder location, always the initial portion of the filename string. Path names end with a slash character and, where appropriate, can begin with a drive letter. Windows paths use backward slashes (\). UNIX and Macintosh paths use forward slashes (/).

Examples

Return the pieces of a file specification string to the separate string outputs pathstr, name, and ext. The full file specification is:

file = 'H:\user4\matlab\classpath.txt';
[pathstr, name, ext] = fileparts(file)

pathstr =
H:\user4\matlab

name =
classpath

ext =
.txt
 

Query parts of a user .cshrc file:

[p,n,e] = fileparts('/home/jsmith/.cshrc')

pathstr =
/home/jsmith

name =
   Empty string: 1-by-0

ext =
.cshrc

fileparts interprets the entire file name as an extension because it begins with a period.

Alternatives

Use uigetfile to interactively select and return a file name and path, or uigetdir to interactively select and return a path name. If you call fileparts with the output of uigetfile, you can parse out the file name and extension.

See Also

filesep | fullfile | pathsep | uigetdir | uigetfile

Tutorials

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


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