This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Parts of file name and path


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


[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 character vector. The returned ext field contains a dot (.) before the file extension.

Input Arguments


Name of a file or folder, specified as a character vector. filename 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


Part of filename interpreted as a path name, specified as a character vector.


Name of the file without any extension, specified as a character vector


File extension, specified as a character vector. ext begins with a period (.)


Return the different parts of a file specification to pathstr, name, and ext. The full file specification is:

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

pathstr =

name =

ext =

Query parts of a user .cshrc file:

[pathstr,name,ext] = fileparts('/home/jsmith/.cshrc')

pathstr =

name =
   Empty matrix: 1-by-0

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

More About

collapse all

Path Name

The full or partial path to a destination folder location, always the initial portion of filename. 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 (/).


  • fileparts only parses file names. It does not verify that a file or a folder exists.

  • You can reconstruct the file from the parts using:

    fullfile(pathstr,[name ext])

  • On Microsoft® Windows systems, you can use either forward (/) or back (\) slashes as path delimiters, even within the same file name. On UNIX and Macintosh systems, use only / as a delimiter. You can use the filesep function to insert the correct separator character for the platform on which your code executes:

    sep = filesep;
    file = ['H:' sep 'user4' sep 'matlab' sep 'myfile.txt'];
    file =

  • If the input consists of a folder name only, be sure that the right-most character is a delimiter (/ or \). Otherwise, fileparts parses the trailing portion of filename as the name of a file and returns it in name instead of in pathstr.


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.

Introduced before R2006a

Was this topic helpful?