Main Content

fileparts

Get parts of file name

Description

example

[filepath,name,ext] = fileparts(filename) returns the path name, file name, and extension for the specified file.

fileparts only parses the specified filename. It does not verify that the file exists.

Examples

collapse all

Get the path, name, and extension of myfile.txt.

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

Get the parts of a user .cshrc file name for a Linux® system.

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

[filepath,name,ext] = fileparts("/home/jsmith/.cshrc")
filepath = 
"/home/jsmith"
name = 
""
ext = 
".cshrc"

Get the file path, name, and extension from each element within a 2x2 string array.

files1_4 = ["H:/user1/matlab/file1.txt",...
    "H:/user2/matlab/file2.txt";"H:/user3/matlab/file3.txt",...
    "H:/user4/matlab/file4.txt"];
[path,name,ext] = fileparts(files1_4)
path = 2x2 string
    "H:/user1/matlab"    "H:/user2/matlab"
    "H:/user3/matlab"    "H:/user4/matlab"

name = 2x2 string
    "file1"    "file2"
    "file3"    "file4"

ext = 2x2 string
    ".txt"    ".txt"
    ".txt"    ".txt"

Input Arguments

collapse all

File name, specified as a string array, character vector, or cell array of character vectors. filename can include a path and file extension.

On Microsoft® Windows® systems, you can use either forward slashes (/) or backslashes (\) as path delimiters, even within the same file name. On UNIX® and Macintosh systems, use only / as a delimiter.

To specify a folder name only, add a trailing delimiter to the filename.

Data Types: char | string | cell

Output Arguments

collapse all

File path, returned as a string array, character vector, or cell array of character vectors. filepath has the same data type and shape as the input argument filename. If the name of the file to parse does not specify a path, filepath is empty ('').

Data Types: char | string | cell

File name, returned as a string array, character vector, or cell array of character vectors. name has the same data type and shape as the input argument filename.

The extension is not included. fileparts interprets all characters following the rightmost delimiter as the name of the file plus extension.

Data Types: char | string | cell

File extension, returned as a string array, character vector, or cell array of character vectors. ext has the same data type and shape as the input argument filename.

ext begins with a period (.). If the name of the file to parse does not specify an extension, ext is empty ('').

Data Types: char | string | cell

Tips

  • To reconstruct a file name from the output of fileparts, use strcat to concatenate the file name and the extension that begins with a period (.) without a path separator. Then, use fullfile to build the file name with the platform-dependent file separators where necessary. For example, fullfile(filepath, strcat(name,ext)).

Introduced before R2006a