fileparts

Returns the directory, name, and extension components of the file filepath.

Syntax

fileparts(filepath)

R1 = fileparts(filepath)

[R1, R2] = fileparts(filepath)

[R1, R2, R3]=fileparts(filepath)

Inputs

filepath
Name or path of a file.
Type: char | string

Outputs

R1
The directory portion of a pathname is always returned.
Type: char | string
R2
When nargout is 2 or more, the R2 is the filename portion of a pathname.
Type: char | string
R3
When nargout is 3 or more, the R3 is the file extension portion of a pathname.
Type: char | string

Example

Example including the fileparts function.

% create a pathname from drive, directories and filename.
pathname = fullfile('c:', 'tests', 'A', 'testscript.oml')

% nargout 0
fileparts(pathname)

% nargout 1 path 
R = fileparts(pathname)

% nargout 2 path and filename
[R1, R2] = fileparts(pathname)

% nargout 3 path, filename and file extension
[R1, R2, R3] = fileparts(pathname)
pathname = c:\tests\A\testscript.oml
ans = c:\tests\A
R = c:\tests\A
R1 = c:\tests\A
R2 = testscript
R1 = c:\tests\A
R2 = testscript
R3 = .oml

Comments

When a pathname does not contain a requested file part, e.g. extension, the nargout variable will be set to empty matrix.