Documentation

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.

fileparts

Get parts of file name

Syntax

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

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

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 =

  1x0 empty char array
ext = 
'.cshrc'

Input Arguments

collapse all

File name, specified as a string scalar or character vector. 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.

Data Types: char | string

Output Arguments

collapse all

File path, returned as a string scalar or character vector. filepath has the same data type 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

File name, returned as a string scalar or character vector. name has the same data type 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

File extension, returned as a string scalar or character vector. ext has the same data type 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

Tips

  • To reconstruct a file name from the output of fileparts, use fullfile. For example, [filepath,filesep,name,ext].

  • To ensure that the correct separator character for the platform is used, use fullfile. For example:

    file = fullfile('H:/user4/matlab/myfile.txt')
    file =
        'H:\user4\matlab\myfile.txt'

  • To specify a folder name only, be sure that the rightmost character in filename is a delimiter (/ or \). Otherwise, fileparts parses the trailing portion of filename as the name of the file and returns it in name instead of in filepath.

Introduced before R2006a

Was this topic helpful?